25 Odd and even random number generator
I need to generate 25 random numbers and then split 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's 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.
source to share
int[] evenNumbers = new int[25];
will create an array of 25 ints, which are initialized to 0.
Then the loop will print out 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 this either loop from 0 to k-1 (for example i < k
) or use List<Integer>
instead of an array int
.
The same goes for odd numbers, you just use instead i < l
.
source to share
When an int array is initialized, it is filled with 0s as the initial value. You have 2 options.
Option 1:
import java.util.ArrayList;
And use ArrayList to store the last even and odd integers. This is not optimal as you are looking for a small number of numbers.
Option 2:
Stop calling System.out.println (); when 0 is reached, 0 is considered an even number, so you have to start printing the odd numbers, count how many are left, and stop at 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] + " ");
}
source to share
Zeros are printed because you created an array of 25 ints
. Each of the 25 is ints
initialized to zeros (the default for a primitive int
in java).
To achieve your goal, you have two options:
- Instead of an array, you can use
ArrayList<Integer>
.ArrayList
is just a resize array inside. OR - You have to include two counters that keep track of how many elements you have inserted into both odd / even arrays.
source to share
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] + " ");
}
}
}
source to share
I suggest you another approach :)
1) You don't need two for loops - one to generate random numbers and the other to separate them odd and even. You can do this with a single for loop. 2) Use List<Integer>
instead of an array. Use an array only if specified by the task.
3) You may not need a variable randomNumbers
.
4) If you use 0 as a special value, be careful. One can get null value from Math.random()
Math.random () and null 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 + " ");
}
}
source to share