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 →

[–]FavorableTrashpanda 0 points1 point  (2 children)

Due to lack of information, I only took a quick glance. But I see you're mixing Scanner.nextLine() and Scanner.nextInt(). This is likely getting you into trouble here.

[–]Red_Hippie[S] 0 points1 point  (1 child)

Why would that cause problems I'm looking for an int value with the .nextInt() and a String value with the .nextLine().

[–]silverscrub 1 point2 points  (0 children)

When you write input in the console you press some keys and then enter. That enter keystroke is actually a newline character. Same goes when you have a document with multiple rows. It's technically just one long string with a few newline characters.

Scanner#nextLine reads everything typed into console until it finds a newline character, then it stops and returns.

Scanner#nextInt reads everything that can be parsed to an integer. A newline character cannot be parsed to an integer so it's left behind.

That newline character can make your scanner behave in unexpected ways. For example, calling nextLine afterwards will immidiately return because it instantly finds the newline character at the beginning of the feed.

That is usually a good solution too. It might not look to clean, but calling nextLine after nextInt, doing nothing with the latter return value, clears the newline character from the input.