The next() method leaves the lineend character from pressing the Enter key in the Scanner class's buffer, the nextLine() method reads everything including the lineend character.
If the nextLine() method is called after the next() method, it will read the lineend that was left in the buffer by next() and return an empty String.
Looking at the JavaDocs, it appears that the next() method "finds and returns the next complete token" where nextLine() looks for everything up to a line separator.
I think that next() saw two tokens - a typed word and a cr character. The cr character triggered the not an option branch.
posted 4 years ago
sorry...so when would you use next and nextline? or do you typically use nextline most of the time
posted 4 years ago
when would you use next and nextline?
Use next when you want to read the next token. Tokens are separated by delimiters like spaces or lineend characters.
Use nextLine when you want to read everything up to and including the lineend character. There can be spaces etc in the line that is read.
Sorry I am late to this discussion because I have been away.
I can see all sorts of confusion. Only use \n or \r if somebody has told you to use the CR or LF characters, never if you want an ordinary line end. Use println(...) or printf("...%n...") instead. Try to join your text to be printed together with the + operator rather than printing individual tokens or lines; you will get slightly faster execution. Your code is much too long to fit into the main method.
You might get different results from a Scanner if you use it at the keyboard from if you use it with a String. There is serious confusion about nextLine in many books, but the API documentation is quite clear: it returns
the rest of the current line
You can get the empty String returned if you use nextLine after nextAnythingElse. There is more explanation and one possible solution to that problem here.
Replace the word "snake" with "danger noodle" in all tiny ads.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop