Java array

Boncz Szilard
Boncz Szilard used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

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;

             }          

        }

    }          
}
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
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Thx
Solved ? How can it be solved ?

Author

Commented:
not solved :(
So why did you close the question?

Author

Commented:
can you help me pls?
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.

Author

Commented:
thx
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
Please post your code inside code tags.
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;

             }     
}
} 
}

Open in new window

Author

Commented:
sorry i don't understood what dou you mean "code tags" im new on this site i think i got it:)
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.");
	
}

}

Open in new window

Author

Commented:
Thank you very much krakatoa you saved me :) nice awesome
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial