Re: How to detect blank input using Scanner?



Taria wrote:
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


Puzzled:

Me too. It seems that Scanner doesn't detect the <ENTER> key being pressed. I don't know why and I don't have much call to use that class so I don't know a lot about it. Take a look at the program below. It does what you want but with different classes. Note the str.equals(""). There is a difference between a null and a "" String.

import java.io.*;

public class test6 {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

String str = br.readLine();
if (str.equals(""))
System.out.println("default");
else
System.out.println(str);
}
}

--

Knute Johnson
email s/nospam/knute/
.



Relevant Pages

  • How to use Log4net TelnetAppender and Trace Listeners
    ... Is there a way to use Log4net and Trace Listeners? ... public static void Initialize(string traceListeners, ... public static void Flush2File(string file, string msg, bool ...
    (microsoft.public.dotnet.framework)
  • Re: must find print packages which can deal with large charts
    ... list available printers, query a named printer, print text and image files ... String inputFileName = null; ... printToFile(outputFileName, outputFileType, ... public static void queryServices{ ...
    (comp.lang.java.gui)
  • nullpointer exception while try to retrieve the elements from the array object
    ... Menu mainMenu = new Menu; ... public static void getConsolidateMenu(Menu menu, Element current, ... mainMenu.setSubMenu(subMenu); ... String description; ...
    (comp.lang.java.programmer)
  • Re: alternative to my ClassLoader hack
    ... public static void main(String... ... Just for the record, this runs, but gets an exception. ... String name: /fubar/MyClassLoader.class ... throws ClassNotFoundException, InstantiationException, ...
    (comp.lang.java.programmer)
  • Re: Get regular expression
    ... "ABLATION LESION HEART BY PERIPHERALLY INSERTED CATHETER 37.34", ... private string _text; ... public Entry(string text, string citation): ... public static void AddToList(string line, string citation, ArrayList ...
    (microsoft.public.dotnet.languages.csharp)