Re: How to detect blank input using Scanner?



We can't tell what you are doing without the code. A small, compilable
test program is always helpful especially if it doesn't do what you want.

Remember to compare Strings use .equals() not ==.

--

Knute Johnson

I thought the .equals would have worked! But it still produces the
same result. I have included a small compilable portion of my
program. All I want it to do is assign 'grades.dat' should the user
hit the enter key.

import java.util.Scanner;

public class Test1 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
System.out.print("Enter the names of your input file:
<grades.dat> ");

// file to be read
String fileNameR = input.nextLine();

// if no input is given, defaults to "grades.dat"
if (fileNameR.equals(null)) {
fileNameR = "grades.dat";
}

System.out.print("\nProcessing " + fileNameR + ". \n");
}
}
// end program

The if statement is never evaluated to be true. Any help is
appreciated.

Puzzled Marion

.