Question

I want to sort integers from an array and put them in separate arrays for negative numbers, odd, and even numbers.

I tried everything i could think of, I'm a beginner and I really need to solve this error.

package intarray;
import java.util.Scanner;


public class Input {

    public static void main(String[] args) {

    int a[] = new int[10];
    int z = 0;
    int x = 0;
    int y = 0;
    int e[] = new int[z];
    int n[] = new int[x];
    int o[] = new int[y];
    int i = 0;

    while (i<10){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter the number");
    a[i] = s.nextInt();
    if (a[i] % 2 == 0 && a[i] > 0 ){ 
        o[y] = a[i];
        y++;
    }
    if (a[i] % 2 != 0 && a[i] > 0 ){
        e[x] = a[i];
        x++;
    }
    if (a[i] < 0){
        e[z] = a[i];
        z++;
    }

    i++;

    }

    System.out.println("Odd numbers: " + o);
    System.out.println("Even numbers: " + e);
    System.out.println("Negative numbers: " + n);

    }

}
Was it helpful?

Solution

Arrays in Java are not resizable, so you need to allocate enough size initially:

int e[] = new int[10];
int n[] = new int[10];
int o[] = new int[10];

Also, you mixed up even and odd numbers. It should be:

if (a[i] % 2 == 0 && a[i] > 0 ){ 
    e[x] = a[i];
    x++;
}
if (a[i] % 2 != 0 && a[i] > 0 ){
    o[y] = a[i];
    y++;
}

Also, you have a typo when storing negative number. Shuld be:

if (a[i] < 0){
    n[z] = a[i];
    z++;
}

And finally you cannot output array as you do. Should be:

System.out.println("Even numbers: " + Arrays.toString (e));
System.out.println("Odd numbers: " + Arrays.toString (o));
System.out.println("Negative numbers: " + Arrays.toString (n));

Here is how I would do this:

public static void main(String[] args) {
    List <Integer> e = new ArrayList<> ();
    List <Integer> o = new ArrayList<> ();
    List <Integer> n = new ArrayList<> ();
    Scanner s = new Scanner(System.in);

    for (int i = 0; i < 10; i++)
    {
        System.out.println("Enter the number");
        int a = s.nextInt ();

        if (a > 0)
        {
            if (a % 2 == 0) e.add (a);
            else o.add (a);
        }
        else n.add (a);
    }

    System.out.println("Even numbers: " + e);
    System.out.println("Odd numbers: " + o);
    System.out.println("Negative numbers: " + n);
}

OTHER TIPS

int z = 0;
int x = 0;
int y = 0;
int e[] = new int[z];
int n[] = new int[x];
int o[] = new int[y];

You're creating arrays with size 0. While the syntax is ok (code will compile), you cannot use your arrays, since they're of size 0 (hence you can't put anything on them). Remember that in Java, the size of an array cannot be changed after initialization.

I would suggest you read up on List, which is basically an array with an arbitrary size (expands to your needs).

For example:

List<Integer> positiveNumbers = new ArrayList<Integer>();
List<Integer> negativeNumbers = new ArrayList<Integer>();
...

You can add items in your List like this:

positiveNumbers.add(<your integer here>);

I know it's a bit of a re-write, but how about this. This way you have a better system that you can extend in the future easily.

package intarray;
import java.util.Scanner;
import java.util.ArrayList;

public class Input {
    private Integer defaultSize = 10;
    ArrayList<Integer> theArray = new ArrayList<Integer>(defaultSize);
    ArrayList<Integer> counts   = new ArrayList<Integer>(3);

    // Here you have your main where you just call stuff in turn.
    // You could also swap in any other method of filling in your array or counting it.
    public static void main(String[] args) {
        getInput();
        count();
        outputCounts();
    }

    public void getInput() {
        for (int index = 0; index < defaultSize; index++) {
            Scanner s = new Scanner(System.in);
            System.out.println("Enter the number");
            theArray.add(s.nextInt();)
        }    
    }
    public void count() {
        for (int i = 0; i < defaultSize; i++) {
            if (theArray.get(i) < 0) { 
                counts.set(counts.get(0) + 1);
            } else if (theArray.get(i) == 0) { 
                counts.set(counts.get(1) + 1);
            } else { 
                counts.set(counts.get(2) + 1);
            }
        }
    }
    public void outputCounts() {
        System.out.println("There were " + counts.get(0) + " negative numbers.");
        System.out.println("There were " + counts.get(1) + " posative numbers.");
        System.out.println("There were " + counts.get(0) + " instances of the number 0."); 
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top