2

I was reading on the difference between Scanner and BufferedReader on stackoverflow.

In one of the answers: https://stackoverflow.com/a/14292918

It was mentioned that there is a difference in

Integer.parseInt(br.readLine())

and

scanner.nextInt();

Which is how new lines are handled.

Can someone expand on this, preferrably with an example ?

Community
  • 1
  • 1
adi rohan
  • 796
  • 1
  • 10
  • 26
  • 1
    `Scanner.nextInt()` does not eat up the new line character at the end of the line so the cursor is still on the same line and doesn't move to the next line. It will only get the next `Integer`. `BufferedReader.readLine()` will read the entire line. – brso05 Jul 31 '15 at 13:43
  • 1
    Is there a difference between `Scanner.readLine()` and `br.readLine()`? – Zion Jul 31 '15 at 13:47

1 Answers1

4

Integer.parseInt(br.readLine()) <-- reads a complete line then convert it to integer

scanner.nextInt(); <-- reads the next token within the input then tries to convert it to integer

NOTE:

Both may throw Exception if the String they find is NOT convertible to Integer

nafas
  • 5,283
  • 3
  • 29
  • 57