Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test for blank line with Java Scanner?

I am expecting input with the scanner until there is nothing (i.e. when user enters a blank line). How do I achieve this?

I tried:

while (scanner.hasNext()) {
    // process input
}

But that will get me stuck in the loop

like image 953
Jiew Meng Avatar asked Sep 06 '11 13:09

Jiew Meng


People also ask

How do you input a blank line in Java?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

Does Java Scanner ignore whitespace?

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. You could use the nextLine() method to get the whole line and not "ignore" with any whitespace.

How do I scan a single line in Java?

The nextLine() method of java. util. Scanner class advances this scanner past the current line and returns the input that was skipped. This function prints the rest of the current line, leaving out the line separator at the end.


3 Answers

Here's a way:

Scanner keyboard = new Scanner(System.in);
String line = null;
while(!(line = keyboard.nextLine()).isEmpty()) {
  String[] values = line.split("\\s+");
  System.out.print("entered: " + Arrays.toString(values) + "\n");
}
System.out.print("Bye!");
like image 125
Bart Kiers Avatar answered Oct 19 '22 23:10

Bart Kiers


From http://www.java-made-easy.com/java-scanner-help.html:

Q: What happens if I scan a blank line with Java's Scanner?

A: It depends. If you're using nextLine(), a blank line will be read in as an empty String. This means that if you were to store the blank line in a String variable, the variable would hold "". It will NOT store " " or however many spaces were placed. If you're using next(), then it will not read blank lines at all. They are completely skipped.

My guess is that nextLine() will still trigger on a blank line, since technically the Scanner will have the empty String "". So, you could check if s.nextLine().equals("")

like image 37
AlexFZ Avatar answered Oct 20 '22 00:10

AlexFZ


The problem with the suggestions to use scanner.nextLine() is that it actually returns the next line as a String. That means that any text that is there gets consumed. If you are interested in scanning the contents of that line… well, too bad! You would have to parse the contents of the returned String yourself.

A better way would be to use

while (scanner.findInLine("(?=\\S)") != null) {
    // Process the line here…
    …

    // After processing this line, advance to the next line (unless at EOF)
    if (scanner.hasNextLine()) {
        scanner.nextLine();
    } else {
        break;
    }
}

Since (?=\S) is a zero-width lookahead assertion, it will never consume any input. If it finds any non-whitespace text in the current line, it will execute the loop body.

You could omit the else break; if you are certain that the loop body will have consumed all non-whitespace text in that line already.

like image 7
200_success Avatar answered Oct 20 '22 00:10

200_success