Take the 2-minute tour ×

I need help understanding how to write a for loop that takes in a certain amount of integers (must be 1 through 10) and it stops taking in numbers once 0 is entered (0 will be the last number). My code so far is:

   import java.util.Scanner;
   public class countNum {

      public static void main(String[] args) {

        int[] array;

        Scanner input = new Scanner(System.in);
        System.out.println ("Enter in numbers (1-10) enter 0 when finished:");

        int x = input.nextInt();

        while (x != 0) {
          if (x > 2 && x < 10) {
          //Don't know what to put here to make array[] take in the values
          //Can I just put break? How do I get it to go back to the top of the while loop?



I don't understand how to simultaneously initialize an array with a set length while having the Scanner read a certain amount of digits of that unknown length, until 0 is entered, and then the loop stops taking in input for the array.

Thanks for any help!

share|improve this question
you may want to use ArrayList it won't require you to set size it does it dynamically anyexample.com/programming/java/java_arraylist_example.xml –  Bhavik Shah Oct 23 '12 at 4:47
initialize an array with a set length -> You don't need to. Just go with an ArrayList that increases its size dynamically. And I see that you have used two different variables representing your number in your condition. –  Rohit Jain Oct 23 '12 at 4:47
Can you guys please go into a bit more detail? –  Zach Oct 23 '12 at 4:48
Change int[] array to List<Integer> array –  smk Oct 23 '12 at 4:51

3 Answers 3

up vote 1 down vote accepted

Ok here's the bit more detail: -

  • You need to use an ArrayList if you want a dynamically increasing array. You do it like this: -

  • Now, in your above code, you can put your number reading statement (nextInt) inside the while loop, since you want to read it regularly. And put a condition in while loop to check whether the entered number is an int or not: -

    int num = 0;
    while (scanner.hasNextInt()) {
        num = scanner.nextInt();
  • Further, you can move on your own. Just check whether the number is 0 or not. If it is not 0, then add it to ArrayList: -

  • If its 0, break out of your while loop.

  • And you don't need that x != 0 condition in your while loop, as you are already checking it inside the loop.

share|improve this answer

In your case, the user seems to be able to input any number of digits. For this scenario, having an array is not ideal simply because the size of the array needs to be known prior to the array initialization. You have some options though:

  1. Use an ArrayList. This a dynamic data structure which expands dynamically.
  2. Ask the user the amount of numbers he/she is going to be entering and use that to initialize the array.
  3. Create an array basing yourself on some assumption on the size.

In both cases 2 and 3, you would also need to include some logic that will make the program stop when: (1) The user enters 0 (2) OR when the amount of numbers provided by the user exceeds the size of the array.

I would recommend sticking to the first solution though since it is easier to implement.

share|improve this answer

I strongly recommend you go get some hands on with Java Collections.

you can fix your program as

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

  public class arrayQuestion {

    public static void main(String[] args) {

        List<Integer> userInputArray = new ArrayList<Integer>();

        Scanner input = new Scanner(System.in);
        System.out.println("Enter 10 Numbers ");
        int count = 0;
        int x;
        try {
            do {
                x = input.nextInt();
                if (x != 0) {
                    System.out.println("Given Number is " + x);
                } else {
                            .println("Program will stop Getting input from USER");
            } while (x != 0 && count < 10);

            System.out.println("Numbers from USER : " + userInputArray);
        } catch (InputMismatchException iex) {
            System.out.println("Sorry You have entered a invalid input");
        } catch (Exception e) {
            System.out.println("Something went wrong :-( ");

        // Perform Anything you want to do from this Array List


I hope this will solve your doubt.. beyond this u need to handle Exceptions if user enters any character or invalid inputs as above

share|improve this answer

Your Answer


