简体   繁体   中英

Handling user input using Scanner

I'm trying to retrieve user input through a single line of input: eg 5,6,4,8,9 using scanner Delimiter for the comma.How do i retrieve an arbitrary amount of integers using this type of input? That is, without having to ask the user how many integers they wish to enter. Here is the code i have been using, but cannot have the while loop break when i want it to break. Note that i keep System.out to keep track of where the program is currently running. The one part that baffles is that, i can get the user inputs in this format, but the program stops and asks for user input once again, then if input is one integer, it asks for more input until user inputs a longer stream of inputs (at least 2,6 + enter) then outputs the string "called break" then "outside of while loop".

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;


public class Lab1 {

    /**
     * @param args
     * @throws IOException 
     * @throws NumberFormatException 
     */
    @SuppressWarnings("resource")
    public static void main(String[] args) throws NumberFormatException, IOException {
        // TODO Auto-generated method stub
        ArrayList<Integer> numbers = new ArrayList<Integer>();
        int numberList;
        Scanner scanner = new Scanner(System.in);
        scanner.useDelimiter(",");
        System.out.print("Enter integers: ");

        while(scanner.hasNext())//------------------------while loop------
        {
            if(scanner.hasNextInt())
            {
                numberList = scanner.nextInt();
                numbers.add(numberList);
                System.out.println(numbers.toString() + " " + numbers.size());
            }
            else 
            {
                System.out.println("called break");
                break;
            }
            System.out.println("Inside of while loop");
        }//-------------------------------------------------end of while loop------

        System.out.println("outside of the while loop now");
    } 
}

Input: 1,2,3,4,5
Enter integers: 1,2,3,4,5
[1] 1
Inside of while loop
[1, 2] 2
Inside of while loop
[1, 2, 3] 3
Inside of while loop
[1, 2, 3, 4] 4
Inside of while loop

I'm still inside the while loop even though there is no integer after the 5 and the programs is waiting for more input, now if i insert a non integer value in the end i get the same exact thing. after these two result, once the user inputs more value (1,2) the program exits the while loop and completes successfully. How do can i make the while loop break once i'm done entering integer using the comma delimiter method?

I know this not answers your question , but it's another way to do the intended:

Here the split(delimiter) method is used, which will return a String[] array. Then in a for loop, use Integer.parseInt() to convert the String s into integers:

public static void main(String[] args)
{
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    int numberList;
    Scanner scanner = new Scanner(System.in);
    scanner.useDelimiter(",");
    System.out.print("Enter integers: ");
    String[] parts = scanner.nextLine().split(",");

    // Add int's to the array 'numbers'
    for (String s : parts) {
        numbers.add(Integer.parseInt(s)); // Convert String to Integer
    }
}

I would recommend reading comma seperated numbers as a String using scanner.nextLine() , then splitting the String using "," , then using Integer.parseInt() to get integer values of numbers. This way, you can input as many numbers as you want without prior definition of "numberCount".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM