Java array

We gave the following string:

int [] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87}.

There must be two rows, one for storage and the other positive elements for the negative elements of string array, and extracting the necessary logic that will perform the appropriate elements and placing them in the appropriate thread.

In other words, in the line array that is given is to be obtained all the positive elements and be placed in a separate row. Also, all the elements to be obtained and the negative to be placed in a separate row.

You also need to determine the number of duplicates in the string array.

Of course, it is necessary functionality written to be applicable to any number of integers. But MY teacher said that my code doesn't have a target row and He gave me an example: Target strings are two additional rows you will place isolated positive and negative values. For example, if you have a string:
int arr = {1,2,3,4, -1, -2, -3, -4}
then the target ranges will be:
int pos = {1,2,3,4}
int hc = {-1, -2, -3, -4}
This outputs needs

Starter array: 12 23 -22 0 43 545 -4 -55 43 12 0 -999 -87

Positive array: 12 23 0 43 545 43 12 0

Negative array: -22 -4 -55 -999 -87

Duplicate array :12 0 43

Thx!!!
2017-04-07.png
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
Somthing like this

public static void main(String[] args) {

int array1[]= {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};

System.out.println("Array 1 :");

Arrays.sort(array1);

for (int positive: array1) {

if (positive >= -1)
System.out.println("Positive numbers :" + positive+ "\t");
}
System.out.println();
System.out.println("Array 2 :");

for (int negative: array1) {
if (negative >= -1) {

}else{ System.out.println("Negative numbers :" +negative);
}

}
System.out.println();

for (int i = 0; i < array1.length -1; i++) {
if (array1[/i + 1 ] == array1) {
System.out.println("Duplicate element found :" + array1
);
i = i + 1;

}

}

}
}
Commented:
We gave the following string:

int [] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87}.

What do you mean String ? It's an Array. Of ints
Author Commented:
Yes
Author Commented:
Thx
Commented:
Solved ? How can it be solved ?
Author Commented:
not solved :(
Commented:
So why did you close the question?
Author Commented:
can you help me pls?
Commented:
You could make two other arrays, one to take negative numbers and the other positive. Put numbers from the original array into these using a loop to test each element for <0 . During that exercise you can have a nested for-loop to test for duplicates, at which point you can do whatever needs to be done with the dupes.
You already used Arrays.sort(), so you can keep or drop that depending how important sort order is to the result.

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
thx
Commented:
Do you want to share your code so that (we) can check it's good ?
Author Commented:
Here but not working correct me pls
Thank you!!!

public static void main(String[] args) {

int array1[]= {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};

System.out.println("Array 1 :");

Arrays.sort(array1);

for (int positive: array1) {

if (positive >= -1)
System.out.println("Positive numbers :" + positive+ "\t");
}
System.out.println();
System.out.println("Array 2 :");

for (int negative: array1) {
if (negative >= -1) {

}else{ System.out.println("Negative numbers :" +negative);
}

}
System.out.println();

for (int i = 0; i < array1.length -1; i++) {
if (array1[/i + 1 ] == array1) {
System.out.println("Duplicate element found :" + array1);
i = i + 1;

}
}
}
}
Author Commented:
i need for an exam
Commented:
Commented:
If you don't post the code inside code tags, I am not prepared to assist you any further.
Author Commented:
``````public static void main(String[] args) {

int array1[]= {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};

System.out.println("Array 1 :");

Arrays.sort(array1);

for (int positive: array1) {

if (positive >= -1)
System.out.println("Positive numbers :" + positive+ "\t");
}
System.out.println();
System.out.println("Array 2 :");

for (int negative: array1) {
if (negative >= -1) {

}else{ System.out.println("Negative numbers :" +negative);
}

}
System.out.println();

for (int i = 0; i < array1.length -1; i++) {
if (array1[/i + 1 ] == array1) {
System.out.println("Duplicate element found :" + array1);
i = i + 1;

}
}
}
}
``````
Author Commented:
sorry i don't understood what dou you mean "code tags" im new on this site i think i got it:)
Commented:
``````import java.util.*;

public class GArr {

static int array1[]= {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
static int o = 0;
static int[] pos = new int[array1.length];
static int[] neg = new int[array1.length];

public static void main(String[] args){

Arrays.sort(array1); //this is not needed really

for(int counter = 0;counter<array1.length;counter++){

if(array1[counter]<0){neg[counter]=array1[counter];}
else if(array1[counter]>0){ pos[counter]=array1[counter];}

}

Arrays.sort(pos);
Arrays.sort(neg);

while(pos[o]==0){o++;}
pos = Arrays.copyOfRange(pos,o,pos.length);
o=0;
while(neg[o]<0){o++;}
neg = Arrays.copyOfRange(neg,0,o);

int[] zer = new int[array1.length-(pos.length+neg.length)];

for(int t=0;t<pos.length;t++){System.out.println(pos[t]);}
for(int t=0;t<neg.length;t++){System.out.println(neg[t]);}

int y=0;
while(y<pos.length-1){if(pos[y]==pos[y+1]){System.out.println("Dupe found "+pos[y]+" "+pos[y+1]);}y++;}
y =0;
while(y<neg.length-1){if(neg[y]==neg[y+1]){System.out.println("Dupe found "+neg[y]+" "+neg[y+1]);}y++;}

System.out.println(zer.length>2?"There are "+(zer.length-1)+" "+"dupes of zero.":"There is also "+(zer.length-1)+" "+"dupe of zero.");

}

}
``````
Author Commented:
Thank you very much krakatoa you saved me :) nice awesome
Commented:
You will have to change the approach to the statement about duplicate zeros, in cases where there are no zeros in the array, but I am sure you can see how to do that.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Storage

From novice to tech pro — start learning today.