#Scanner Not working

23 messages · Page 1 of 1 (latest)

neat marlinBOT
#

This post has been reserved for your question.

Hey @scarlet grove! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

#

If Scanner#nextLine reads an empty line after calling a different Scanner#next method, take a look at this StackOverflow post.

scarlet grove
limpid zephyr
#

Did you read the StackOverflow post?

scarlet grove
#

Yeh, still dont get it

limpid zephyr
#

you called nextInt()

#

And then you call nextLine()

#

nextLine tries to read the remainder of the line with the nextInt call

#

which is an empty line

scarlet grove
#

So how does one fix that?

limpid zephyr
#

Add an additional scanner.nextLine(); before System.out.println("Adjective: ");

#

this consumes the remaining line

scarlet grove
#

I know what to do now but I am having a difficult time understand how it works

limpid zephyr
#

ok when you call nextInt, the scanner reads an int

#

but it's still stuck at the same line

#

so when calling nextLine, it finishes the line where it read the int

#

and returns the remainder of that line (which is empty)

#

only when calling nextLine a second time, you are reading the actual line

scarlet grove
#

Ok thanks

#

I understand now

#

Much appreciated thumbsupcat

neat marlinBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.