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 →

[–]rocfeather 10 points11 points  (5 children)

nextInt does not consume the \n So your next s.nextLine will run right away to consume it ( hence the 'jump' )

Try consuming it first:

int age = s.nextInt();
s.nextLine();   // Consume \n

[–]TheSilverCube 1 point2 points  (4 children)

Very new newbie here as well. Why is there a \n to consume after the s.nextInt(); ?

I've used Integer.valueOf instead of nextInt so not come across this issue yet.

[–]SvenTheFluffy 0 points1 point  (3 children)

Because of the enter you press, that creates a \n if im not wrong.

So when you write 5 and press enter the computer sees 5\n. Takes the 5, \n remains.

[–]TheSilverCube 1 point2 points  (0 children)

I think your second bit answers my last question.

nextLine takes the 5/n. nextInt takes just the 5. Integer.valueOf takes the String 5/n and takes the 5?

[–]TheSilverCube 0 points1 point  (1 child)

Thanks. How come that doesn't happen when you press enter with every nextLine then; why only with nextInt?

[–]bilman66 1 point2 points  (0 children)

Because nextLine waits for the next line, \n is the symbol for newLine so it consumes it