Reputation: 2932
So I've been trying to get an array of values from a scanner and fill them in array. If the user enters 10 integer values or -1 the loop ends and the array stops filling in values. The following is the code I have written so far to do this:
// Input scanner and input controls
Scanner scan = new Scanner(System.in);
System.out.print("Please enter 10 integers \n");
int maxnum = 10;
boolean done = false;
int count = 0;
int[] arrays = new int[maxnum];
// Store values in count array
for (count = 0; (count < maxnum && !done); count++) {
arrays[count] = scan.nextInt();
if (arrays[count] == -1) {
arrays[count] = 0;
done = true;
}
}
// print count values
for (count = 0; (count < maxnum); count++) {
if (arrays[count] != 0) {
System.out.println(arrays[count]);
}
}
What I have done so that -1 is not stored in the array is set the value of -1 to 0 and then tell my next loop not to print any values of 0. I know there has to be a better way to do this and I understand that I am still storing the zero values, but for the life of me I can not figure out how to prevent the loop from storing zero values in the array
Upvotes: 2
Views: 22739
Reputation: 20163
This stores only numbers that are not -1. When -1 is entered, the loop is terminated.
import java.util.Scanner;
/**
<P>{@code java TenIntsOrNeg1}</P>
**/
public class TenIntsOrNeg1 {
public static final void main(String[] ignored) {
int maxInts = 10;
int[] ints = new int[maxInts];
Scanner scan = new Scanner(System.in);
System.out.println("Please enter 10 integers, or -1 to quit.");
int idx = 0;
//Keep going for all ten elements...
while(idx < 10) {
System.out.print((idx + 1) + ": ");
int inputNum = scan.nextInt(); //Assumes it *IS* an int
//...unless they enter -1 to terminate.
if(inputNum == -1) {
//End-condition. Number is not stored
break;
}
//Not -1. Store it.
ints[idx++] = inputNum;
}
System.out.println("DONE. Printing:");
for(int i = 0; i < ints.length; i++) {
System.out.println(i + ": " + ints[i]);
}
}
}
Output:
[C:\java_code\]java TenIntsOrNeg1
Please enter 10 integers, or -1 to quit.
1: 6
2: 1
3: 2
4: 8
5: 121
6: -1
DONE. Printing:
0: 6
1: 1
2: 2
3: 8
4: 121
5: 0
6: 0
7: 0
8: 0
9: 0
Upvotes: 1
Reputation: 972
how about storing the input to another variable and checking before populating to the array?
for (count = 0; (count < maxnum && !done); count++) {
int testnum = scan.nextInt();
if (testnum == -1) {
done = true;
}
else {
arrays[count] = testnum;
}
}
Upvotes: 2
Reputation: 433
Primitive data types (including their arrays) are always assigned to their default value if another assignment hasn't happened before referencing. In the case of int
, it's default value is 0 (same with byte
, short
, char
, long
), and 0.0 for float
and double
. Object instance's default value is null
. Because you are using an int[]
array, all of the objects within it are automatically assigned to 0 if you haven't done so already.
But since you do not want to store a 0 in the array, then don't. Use a different number, -1 is fine for that, unless you have any other preferences.
Upvotes: 1
Reputation: 726599
Since the number of entries may be less than maxnum
, a proper way of dealing with the problem would be to store the actual number of numbers that the user has entered, not counting the -1
, and then stop the second loop before the last number is printed. This way you wouldn't care if -1
is in the array or not, because it wouldn't get printed anyway.
You already keep the count
, all you need to do is using it in your second loop:
for (int i = 0; i < count ; i++) {
System.out.println(arrays[i]);
}
Upvotes: 3