

So when you want to ask for number and then for entire line while avoiding that empty string as result of nextLine, either But since line separators after reading the number from the console are found immediately in Scanner's cache, it returns empty String, meaning that Scanner was not able to find any character before those line separators (or end of stream).īTW nextLine also consumes those line separators. Now Scanner#nextLine() simply collects and returns all characters until it finds line separators (or end of stream). What we need to remember, is that all of the Scanner methods are always scanning starting from the cached text. It will read them from System.in (how else Scanner would know that there are no more digits from the user which represent age value than facing whitespace?) which will remove them from standard input, but it will also cache those line separators internally. Scanner#nextInt (and other Scanner#next Type methods) doesn't allow Scanner to consume these line separators. So when you are asking the user for value like age, and user types 42 and presses enter, standard input will contain "42\r\n".
#Scanner sombre controls messing up windows#
What is important is that this key beside ensuring placing user data to standard input (represented by System.in which is read by Scanner) also sends OS dependant line separators (like for Windows \r\n) after it. To do so, the user is required to press "enter"/"return" key on the keyboard. When you are reading data from the console, it allows the user to type his response and when he is done he needs to somehow confirm that fact. Line feed (LF - in String literals represented as "\n") Text which represents few lines also contains non-printable characters between lines (we call them line separators) likeĬarriage return (CR - in String literals represented as "\r") (b) throwing in case of terminated/ended source of data (like File or String) (a) waiting for matching sequence - in case of still opened source of data like System.in but there was no line separator found ? will make \R line separator sequence optional - which will prevent skip method from Use scanner.skip("\\R?") before scanner.nextLine() if you are not sure if previous instruction was scanner.next() or scanner.next*TypeName*().

If I delete it, then both string1 = input.nextLine() and string2 = input.nextLine() are executed as I want them to be. I tested my application and it looks like the problem lies in using input.nextInt().


and this line is executed and waits for my input The problem is that after entering the numerical value, the first input.nextLine() is skipped and the second input.nextLine() is executed, so that my output looks like this: Enter numerical valueĮnter 1st string // The program is supposed to stop here and wait for my input, but is skippedĮnter 2nd string //. String string2 = input.nextLine() // Read 2nd string (this appears right after reading numerical value) String string1 = input.nextLine() // Read 1st string (this is skipped) Option = input.nextInt() // Read numerical value from input It looks like this: ("Enter numerical value") I am using the Scanner methods nextInt() and nextLine() for reading input.
