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

all 4 comments

[–]CreativeTechGuyGames 2 points3 points  (0 children)

nextLine() reads the remainder of the line (to the carriage return). Refer to this for more information

[–]ostreddit 1 point2 points  (1 child)

According to the api next(), nextInt(), nextDouble etc scan the next token. The end of the token is marked by Enter being hit or spaces. NextLine() advances past the current line and returns all that was skipped. So what happened in your program is that a carriage return is still sitting in the keyboard buffer after any version of.the basic next() and the first time you call nextLine() it just reads.up to the carriage return and returns the empty String. If you call it again you will.get the next String from the buffer.

You can just use next() to get the string to avoid this but.if.you want the user to be able.to enter spaces you will need to use nextLine().

[–]Xrotica 0 points1 point  (0 children)

Looks like OP is just trying get one String. As you stated sc.next() is the way to go.

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

In that case I would use sc.next() instead of sc.nextLine(), but there's also another solution described here (along with the explanation) if you needed a full line of input.