3

I need to generate 25 random numbers and then separate them into two arrays, depending on whether they are odd or even. This is my expected output:

Even: 40 10 70 58 20 18 74 44 14 54 50 42
Odd: 21 75 29 69 7 41 33 35 21 13 93 73 33

This is what I have so far:

public static void main(String[] args) {   
Scanner input = new Scanner(System.in).useDelimiter("\n");
int[] randomNumbers = new int[25];
int[] evenNumbers = new int[25];
int[] oddNumbers = new int[25];
int k = 0, l = 0;

for (int i= 0; i< randomNumbers.length; i++) {
    randomNumbers[i] = (int) (Math.random() * 99);
}
for (int i = 0; i < 25; i++) {
    if (randomNumbers[i] % 2 == 0) {
        evenNumbers[k] = randomNumbers[i];
        k++;
    } else {
        oddNumbers[l] = randomNumbers[i];
        l++;
    }
}System.out.print("Even: ");
for (int i = 0; i< evenNumbers.length; i++) {
    System.out.print(evenNumbers[i] + " ");

}System.out.println("");
System.out.print("Odd: ");
for (int i= 0; i < oddNumbers.length; i++) {
    System.out.print(oddNumbers[i] + " ");
    }
  }
}

Here is my actual output:

Even: 40 10 70 58 20 18 74 44 14 54 50 42 0 0 0 0 0 0 0 0 0 0 0 0 0
Odd: 21 75 29 69 7 41 33 35 21 13 93 73 33 0 0 0 0 0 0 0 0 0 0 0 0

I would like to know how to get rid of the zeros at the end of each output.

Kröw
  • 504
  • 2
  • 13
  • 31
aCode31
  • 41
  • 1
  • 3
  • The zeros are there because you have created an array of 25 ints which is initialized to zeros. You can use a `ArrayList` instead to avoid that (which is a resizing array internally).. or have two counters that keep track of how many elements you have inserted in both the odd/even arrays. – Faheem Apr 18 '17 at 13:51
  • Possible duplicate of [How can I dynamically add items to a Java array?](http://stackoverflow.com/questions/5061721/how-can-i-dynamically-add-items-to-a-java-array) – Alex Apr 18 '17 at 13:55
  • https://stackoverflow.com/questions/5061721/how-can-i-dynamically-add-items-to-a-java-array create a dynamic arrayList – Alex Apr 18 '17 at 13:55

5 Answers5

3

int[] evenNumbers = new int[25]; will create an array of 25 ints that are initialized to 0.

The loop will then print all 25 of those ints, even if they haven't been replaced:

for (int i = 0; i< evenNumbers.length; i++) {
  System.out.print(evenNumbers[i] + " ");
}

To fix that either loop from 0 to k-1 (e.g. i < k) or use List<Integer> instead of an int array.

The same applies to the odd numbers, you'd just use i < l instead.

Thomas
  • 87,414
  • 12
  • 119
  • 157
2

When an int array is initiated, it is filled with 0s as the initial value. You have 2 options.

Option 1:

import java.util.ArrayList;

And use an ArrayList to store the latter even and odd integers. This is not an optimal solution as you are looking for a small amount of numbers.

Option 2:

Stop calling the System.out.println(); function when a 0 is reached. 0 is considered an even number, so you need to start printing the odd numbers, count how many there are and stop at a 0 and then start printing the remaining even numbers. Code:

int count = 0;
System.out.print("Odd: ");
for (int i= 0; i < oddNumbers.length; i++) {
    if (oddNumbers[i]!=0) {
        System.out.print(oddNumbers[i] + " ");
        count++;
    }
}
System.out.print(System.lineSeparator()+"Even: ");//Uses the system-specific new-line character which is extracted with System.lineSeparator().
for (int i = 0; i< evenNumbers.length; i++) {
    count++;
    if (count==26) {
        break;
    }
    System.out.print(evenNumbers[i] + " ");
}
1

The zeros are printed because you have created an array of 25 ints. Each of these 25 ints are initialized to zeros(the default value for primitive int in java).

To achieve your goal you have two options:

  1. You can use an ArrayList<Integer> instead of an array. ArrayList is just a resizing array internally.. OR
  2. You have to include two counters that keep track of how many elements you have inserted in both the odd/even arrays.
Faheem
  • 1,423
  • 2
  • 13
  • 22
1
package test2;

import java.util.Scanner;

public class StackO1 {

    public static void main(String[] args) {   
        Scanner input = new Scanner(System.in).useDelimiter("\n");
        int[] randomNumbers = new int[25];
        int[] evenNumbers = new int[25];
        int[] oddNumbers = new int[25];
        int k = 0, l = 0;

        for (int i= 0; i< randomNumbers.length; i++) {
            randomNumbers[i] = (int) (Math.random() * 99);
        }
        for (int i = 0; i < 25; i++) {
            if (randomNumbers[i] % 2 == 0) {
                evenNumbers[k] = randomNumbers[i];
                k++;
            } else {
                oddNumbers[l] = randomNumbers[i];
                l++;
            }
        }System.out.print("Even: ");
        for (int i = 0; i< k; i++) {
            System.out.print(evenNumbers[i] + " ");

        }System.out.println("");
        System.out.print("Odd: ");
        for (int i= 0; i < l; i++) {
            System.out.print(oddNumbers[i] + " ");
            }
          }
        }
DavidG
  • 24,279
  • 14
  • 89
  • 82
0

I would suggest you a different approach :)
1) You don't need two for loops - one to generate random numbers and the second to separate them in odd and even. You can do this with one the for-loop.
2) Use List<Integer> instead array. Use array only if it is requered by the task.
3) Perhaps, you don't need randomNumbers variable.
4) If you use 0 as special value be careful. It is possible to get zero value from Math.random() Math.random() and zero value.

So this is the code

public static void main(String[] args) {
    int NUM_OF_NUMS = 25;

    List<Integer> randomNumbers = new ArrayList<>();
    List<Integer> evenNumbers = new ArrayList<>();
    List<Integer> oddNumbers = new ArrayList<>();

    for (int i = 0; i < NUM_OF_NUMS; i++) {
        int randomNumber = (int) (Math.random() * 99);
        randomNumbers.add(randomNumber);

        if (randomNumber % 2 == 0) {
            evenNumbers.add(randomNumber);
        } else {
            oddNumbers.add(randomNumber);
        }
    }

    printArrays(evenNumbers, oddNumbers);
}

private static void printArrays(List<Integer> evenNumbers, List<Integer> oddNumbers) {
    System.out.print("Even: ");
    for (int num : evenNumbers) {
        System.out.print(num + " ");
    }
    System.out.println("");

    System.out.print("Odd : ");
    for (int num : oddNumbers) {
        System.out.print(num + " ");
    }
}
Community
  • 1
  • 1
djm.im
  • 3,295
  • 4
  • 30
  • 45