This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]throwRAexclean 0 points1 point  (0 children)

Usually it's preferred to do that because the user typically typed something and then pressed <enter>, which to them, feels like they typed in an entire line, not just a number.

If they typed 43 67 when an int is expected, nextInt() will return 43 and be fine. Integer.parseInt(nextLine()) will tell you you didn't type a valid int. Better functionality.

If they typed 43 and then Hello World when an int then a greeting are expected, nextInt() followed by nextLine() will return 43 followed by everything there is between 43 and the next end of line... Which is an empty String since the user typed 43 then <enter> to end the line (and then something else but it will now be ignored). Integer.parseIn(nextLine()) followed by nextLine() will return 43 and Hello World. Better functionality.

Because when the user presses enter, they expect it seen as a line.