Reputation: 11
I'm having issues printing the array in full. The code is set up to print a sequence of random numbers, and the number of random number sequences (1-12) is intended to come from user input. However, I can't get it to print more than the first random number. I managed to print the entire sequence before, but since this is an assignment and I have to use arrays, my first method no longer works.
import java.util.Scanner;
import java.util.Arrays;
public class Test4 {
public static void main(String[] args) {
int randomNumber;
int[] allNumbers = new int[12];
Scanner keyboard = new Scanner(System.in);
for (int i = 1; i <= allNumbers.length; i++) {
allNumbers[i] = (int) (Math.random() * 999 + 1);
System.out.print("How many random numbers? ");
randomNumber = keyboard.nextInt();
System.out.print("\n" + "Random numbers: ");
System.out.print(allNumbers[i] + " ");
{
System.out.print("\n");
System.out.print("\n" + "Even numbers: " + (allNumbers[i] % 2 == 0));
System.out.print("\n" + "Odd numbers: ");
break;
}
}
}
}
New code: Work in progress...
*Update2: Added limit to keyboard.nextint.
Encountered "string literal is not closed by double quote" error at the part where I try to print odds and evens.
*Update3: fixed error above (caused by typo).
New issue: the sorted numbers (odd/even) show up, but in the wrong format. They show up like this: Odd: 475 Odd: 123 even: 62 even: 680 even: 870 odd:457 etc. I want: Even numbers: number number number number Odd numbers: number number number number
Also looking to put a counter in there that counts the number of even/odd random numbers - is there a way of printing all that in one single print? Or do I first have to make a system.out.print followed by a system out printf for the counter?
Updated code:
import java.util.Scanner;
import java.util.Arrays;
public class Test7 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("How many random numbers?: ");
int size = keyboard.nextInt();
System.out.print("\n");
while (size>12 || size<1);
int[] randNum = new int[size];
for(int i = 0 ; i < randNum.length ; i++)
randNum[i] = (int)(Math.random() * 999 + 1);
System.out.println("Random numbers: " + Arrays.toString(randNum).replace("]", "").replace(",", "").replace("[", "") + "\n");
for(int i = 0 ; i < randNum.length ; i++)
if(randNum[i] % 2 == 0) {
System.out.print("\r" + "Even numbers: ");
System.out.print(randNum[i] + " ");
} else {
System.out.print("\r" + "Odd numbers: ");
System.out.print(randNum[i] + " ");
}
}}
Upvotes: 0
Views: 204
Reputation: 5403
Please read through the following code. Looking at the block you have started after this line System.out.print(allNumbers[i] + " ");
, it appears that your concepts need a bit of clearing. The comments are there to help you understand better.
Since you didn't mention anything about the even/odd stuff, I inferred that you are trying to count the number of even and odd numbers, as you will find in the code. Let me know if this needs changing.
Please let me know if there are any questions.
public static void main(String[] args) {
// how many random numbers
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter array size: ");
int size = Integer.parseInt( keyboard.next() );
int[] randNum = new int[size];
// fill array with random numbers
for(int i = 0 ; i < randNum.length ; i++)
randNum[i] = (int)(Math.random() * 999 + 1);
// to print the array, put import java.util.Arrays; at the top
System.out.println(Arrays.toString(randNum));
keyboard.close();
// go over elements looking for even/odd numbers
// by going over the list 2 times, you can print the even in the
// first iteration, odd in the second iteration
// i am using auxiliary lists
// ZERO-th WAY
List<Integer> even = new ArrayList<Integer>();
List<Integer> odd = new ArrayList<Integer>();
for(int i = 0 ; i < randNum.length ; i++) {
// even number found
if(randNum[i] % 2 == 0)
even.add(randNum[i]);
else
odd.add(randNum[i]);
}
System.out.println("Even: " + even);
System.out.println("Odd: " + odd);
// FIRST WAY
// go over elements looking for even/odd numbers
// by going over the list 2 times, you can print the even in the
// first iteration, odd in the second iteration
System.out.print("Even: ");
for(int i = 0 ; i < randNum.length ; i++) {
// even number found
if(randNum[i] % 2 == 0)
System.out.print(randNum[i] + " ");
}
System.out.println();
System.out.print("Odd: ");
for(int i = 0 ; i < randNum.length ; i++) {
// even number found
if(randNum[i] % 2 == 1)
System.out.print(randNum[i] + " ");
}
System.out.println();
// SECOND WAY
int evenCount = 0, oddCount = 0;
for(int i = 0 ; i < randNum.length ; i++) {
// even number found
if(randNum[i] % 2 == 0)
evenCount++;
else
oddCount++;
}
int[] evenNums = new int[evenCount];
int[] oddNums = new int[oddCount];
int evenNumsIndx = 0, oddNumsIndx = 0;
for(int i = 0 ; i < randNum.length ; i++) {
// even number
if(randNum[i] % 2 == 0) {
evenNums[evenNumsIndx] = randNum[i];
evenNumsIndx++;
} else {
oddNums[oddNumsIndx] = randNum[i];
oddNumsIndx++;
}
}
System.out.println("Even: " + Arrays.toString(evenNums));
System.out.println("Odd: " + Arrays.toString(oddNums));
}
Sample output:
Enter array size: 12
[435, 420, 334, 248, 143, 340, 233, 940, 187, 315, 228, 132]
Even: [420, 334, 248, 340, 940, 228, 132]
Odd: [435, 143, 233, 187, 315]
Upvotes: 0
Reputation: 63
First of all, if you want the user to choose the number of random numbers, you will have to get this BEFORE you create the allNumbers
array.
Second, like Imesha Sudasingha said, Java is zero based so the int i needs to be initialized to 0 to avoid an ArrayIndexOutOfBoundException
.
Third, the brackets in the last part of the for
loop does noting for you here. They can be removed without changing the result.
Furthermore you have a break at the end that basically stops the for
loop after one iteration.
I would recommend you start somewhat from scratch and try to set up you program in this order:
allNumbers
array with the correct size. for
loop that ONLY assigns random numbers to the allNumbers
array.for
loop to print the results. Upvotes: 1
Reputation: 157
Everything that Imesha said plus this statement :
{
System.out.print("\n");
System.out.print("\n" + "Even numbers: " + (allNumbers[i] % 2 == 0));
System.out.print("\n" + "Odd numbers: ");
break;
}
System.out.print("\n" + "Even numbers: " + (allNumbers[i] % 2 == 0));
//will print true or false, if you want to display the even and odd number, you should try something like this :
if(allNumbers[i] % 2 == 0) {
System.out.println("Even numbers " + allNumbers[i]");
} else {
System.out.println("Odd numbers " + allNumbers[i]");
}
Upvotes: 1
Reputation: 3570
Java arrays use zero based index.
for(int i = 0; i < allNumbers.length ; i++)
Therefore, the for loop should be as above. You should probably get ArrayIndexOutOfBoundException otherwise.
Also, the break
statement you have included in the for loop cause the loop not to run more than one time. You should always use a break statement with a condition (ex: within an if block)
Upvotes: 2