Re: How to detect blank input using Scanner?
- From: Knute Johnson <nospam@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 28 Jan 2007 14:18:35 -0800
Taria wrote:
We can't tell what you are doing without the code. A small, compilabletest 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/
.
- Follow-Ups:
- Re: How to detect blank input using Scanner?
- From: Marion
- Re: How to detect blank input using Scanner?
- References:
- How to detect blank input using Scanner?
- From: Taria
- Re: How to detect blank input using Scanner?
- From: Knute Johnson
- Re: How to detect blank input using Scanner?
- From: Taria
- How to detect blank input using Scanner?
- Prev by Date: Re: How to detect blank input using Scanner?
- Next by Date: Re: How to detect blank input using Scanner?
- Previous by thread: Re: How to detect blank input using Scanner?
- Next by thread: Re: How to detect blank input using Scanner?
- Index(es):
Relevant Pages
|