2

I'm trying to retrieve user input through a single line of input: e.g 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?

2 Answers 2

2

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 Strings 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
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This seemed to work really great. i got the length of parts and found it to be the correct size. Does this mean that all of the "," characters are removed from the array list?
1

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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.