Re: How to detect blank input using Scanner?



Knute Johnson wrote:
There is a difference between a null and a "" String.

Marion wrote:
But from your code, it sparked a great idea of a solution, I substituted "null" to "" and voila...it works with the Scanner class.

That follows from the documented behavior of java.util.Scanner.nextLine().

Scanner input = new Scanner(System.ini); // did you mean System.in?
String fileNameR = input.nextLine();

<http://java.sun.com/javase/6/docs/api/java/util/Scanner.html#nextLine()>

"Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end."

Since you have a line separator in the input, you have a non-null current line. Assuming UNIXesque input, that line is "\n". Exclude the line separator at the end and you have "". QED.

A similar situation pertains with java.io.BufferedReader.readLine(), prior to end of stream.

<http://java.sun.com/javase/6/docs/api/java/io/BufferedReader.html#readLine()>

- Lew
.