Reputation: 19
Currently I have a while loop that reads through an input file simply named "input". This input file contains numbers, such as "1 2 3 4" that represent something that is irrelevant to my question. To receive the number "1", I use scanner.nextInt();
If this is the correct way to receive 1, I am stuck on how to receive the other numbers 2 3 4.
while(scanner.hasNext()){
int firstnum = scanner.nextInt();
// How do I get the second, third, and fourth number?
//It is not guaranteed that I will be given exactly 4 numbers, but I will be given at least that many.
Upvotes: 0
Views: 37
Reputation: 311978
Just continue the loop and accumulate the numbers you encounter, e.g., to a list:
List<Integer> numbers = new LinkedList<>();
while(scanner.hasNext()) {
numbers.add(scanner.nextInt());
}
Upvotes: 1