How can I read the input (numbers) using the java.util.Scanner
class when the numbers are separated by any number of line breaks?
An example might be like the following:
23333
.
.
.
332332
.
.
.
3333
How can I read the input (numbers) using the java.util.Scanner
class when the numbers are separated by any number of line breaks?
An example might be like the following:
23333
.
.
.
332332
.
.
.
3333
You can set multiline delimeter. Assuming you read your numbers from a file :
Scanner scanner = new Scanner(new File("test.txt"));
scanner.useDelimiter("[\r\n]+"); // for unix "\n" and windows "\r\n" endings
while (scanner.hasNextInt()) {
int x = scanner.nextInt();
System.out.println("read number = " + x);
}
scanner.close();