Using a For loop to populate an array

21,766

Solution 1

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;
    }

}

Solution 2

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]);
}

Solution 3

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

Solution 4

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.

Share:
21,766
Bhetzie
Author by

Bhetzie

Updated on March 07, 2020

Comments

  • Bhetzie
    Bhetzie about 4 years

    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

  • Bhetzie
    Bhetzie about 10 years
    That actually does work pretty well! I wrote something almost exactly identical earlier today but I had included a print statement as well which I think got me confused. Thank you for this!
  • Bhetzie
    Bhetzie about 10 years
    ohhh ok! This makes sense to me. One issue I still have with this code is that a -1 value ends the loop but a value is still entered for it; however, like you said it stores a 0 by default so I could just choose not to print it. Thank you for this!