-2

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
The Guy with The Hat
  • 9,689
  • 7
  • 51
  • 69
GuruMan94
  • 1
  • 1
  • 3
    why won't you try googling it first? there already are zyllions of questions (and answers) about it. possible duplicate of [How to use the Scanner class in java?](http://stackoverflow.com/questions/11871520/how-to-use-the-scanner-class-in-java) – alex Apr 26 '14 at 22:15
  • 1
    @GuruMan94 where do you want to read you data from ? A file ? – kiruwka Apr 26 '14 at 22:34

1 Answers1

1

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();
kiruwka
  • 8,612
  • 3
  • 27
  • 40