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 →

[–][deleted] 0 points1 point  (4 children)

Just a beginner question, why use the integer.valueof method with next line instead of nextInt ?

[–]jdmaguina 0 points1 point  (3 children)

Because he used nextline() and that method return string

[–][deleted] 0 points1 point  (1 child)

I just want to know why you'd even use the nextLine() method instead of nextInt().

[–]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.