Accepting multiple integers on a single line using Scanner
Solution 1
Using the Scanner.nextInt()
method would do the trick:
Input:
56 83 12 99
Code:
import java.util.Scanner;
class Example
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int[] numbers = new int[4];
for(int i = 0; i < 4; ++i) {
numbers[i] = sc.nextInt();
}
}
}
At @user1803551's request on how Scanner.hasNext()
can achieve this:
import java.util.*;
class Example2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
ArrayList<Integer> numbers = new ArrayList<Integer>();
while (sc.hasNextInt()) { // this loop breaks there is no more int input.
numbers.add(sc.nextInt());
}
}
}
Solution 2
The answer by Makoto does what you want using Scanner#nextLine
and String#split
. The answer by mauris uses Scanner#nextInt
and works if you are willing to change your input requirement such that the last entry is not an integer. I would like to show how to get Scanner#nextLine
to work with the exact input condition you gave. Albeit not as practical, it does have educational value.
public static void main(String[] args) {
// Preparation
List<Integer> numbers = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter numbers:");
// Get the input
while (scanner.hasNextInt())
numbers.add(scanner.nextInt());
// Convert the list to an array and print it
Integer[] input = numbers.toArray(new Integer[0]);
System.out.println(Arrays.toString(input));
}
When giving the input 10 11 12
upon first prompt, the program stores them (Scanner
has a private
buffer), but then keeps asking for more input. This might be confusing since we give 3 integers which loop through hasNext
and expect that when the 4th call is made there will be no integer and the loop will break.
To understand it we need to look at the documentation:
Both
hasNext
andnext
methods [and their primitive-type companion methods] may block waiting for further input. Whether ahasNext
method blocks has no connection to whether or not its associatednext
method will block.
(emphasis mine) and hasNextInt
Returns:
true
if and only if this scanner's next token is a valid int value
What happens is that we initialized scanner
with an InputStream
, which is a continuous stream of data. On the 4th call to hasNextInt
, the scanner "does not know" if there is a next int or not because the stream is still open and data is expected to come. To conclude from the documentation, we can say that hasNextInt
Returns
true
if this scanner's next token is a valid int value, returnsfalse
if it is not a valid int, and blocks if it does not know what the next token is.
So what we need to do is close the stream after we got the input:
// Get the input
numbers.add(scanner.nextInt());
System.in.close();
while (scanner.hasNextInt())
numbers.add(scanner.nextInt());
This time we ask for the input, get all of it, close the stream to inform scanner
that hasNextInt
does not need to wait for more input, and store it through iteration. The only problem here is that we closed System.in
, but if we don't need more input it's fine.
chopper draw lion4
Updated on July 09, 2022Comments
-
chopper draw lion4 almost 2 years
The user needs to enter a certain number of integers. Rather them enter an integer at a time, I want to make it so they can enter multiple integers on a single line, then I want those integers to be converted in an array. For example, if the user enters:
56 83 12 99
then I want an array to be created that is{56, 83, 12, 99}
In other languages like Python or Ruby I would use a
.split(" ")
method to achieve this. No such thing exist in Java to my knowledge. Any advice on how to accept user input and create an array based on that, all on a single line?