Home » Java » java – How to close a while loop for an ArrayList that is collecting user input-Exceptionshub

java – How to close a while loop for an ArrayList that is collecting user input-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

The question sounds simple enough but I have spent 2 hours looking for the answer and I can’t seem to find it. I am trying to make a program that populates an array list with user input (of integers) and then closing it when the number 0 is typed.

So far I have:

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.*;
/**
 * Write a description of class ArrayList2 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */

public class Numbers

{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);

        System.out.println ("Enter a list of integers (Enter 0 when done): ");

        ArrayList<Integer> fullArray = new ArrayList<Integer>();

        while (keyboard.hasNextInt())
        {
            fullArray.add(keyboard.nextInt());
            System.out.println (fullArray);


        }

        System.out.println (fullArray);
    }
}

My issue isn’t getting the user input, but closing the arraylist when 0 is typed. I know that hasNextInt returns true as long as their’s another int but 0 is also an int so it will not close the arraylist. I have tried simply saying if(keyboard.nextInt == 0), but that breaks my code and only adds every other number to my arraylist. If anyone can help me it would be much appreciated. thanks.

How to&Answers:

Instead of adding a new nextInt() which will make you skip every other value, simply store the nextInt you already used into a variable (in this case nextValue), check to see if it is 0, break from the loop if it is, otherwise you add it to the List.

Here is the working version below:

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println ("Enter a list of integers (Enter 0 when done): ");
    ArrayList<Integer> fullArray = new ArrayList<>();

    while (keyboard.hasNextInt()) {
        int nextValue = keyboard.nextInt();
        if (nextValue == 0) {
            break;
        }
        fullArray.add(nextValue);
        System.out.println (fullArray);
    }
}

Answer:

Do it as follows:

import java.util.ArrayList;
import java.util.Scanner;

public class Numbers {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter a list of integers (Enter 0 when done): ");

        ArrayList<Integer> fullArray = new ArrayList<Integer>();
        int x = -1;
        while (x != 0 && keyboard.hasNextInt()) {
            x = keyboard.nextInt();
            if (x != 0) {
                fullArray.add(x);
                System.out.println(fullArray);
            }
        }

        System.out.println(fullArray);
    }
}

A sample run:

Enter a list of integers (Enter 0 when done): 
10
[10]
20
[10, 20]
30
[10, 20, 30]
0
[10, 20, 30]

Another sample run:

Enter a list of integers (Enter 0 when done): 
10
[10]
20
[10, 20]
a
[10, 20]