Home > Archive > Java Help > January 2007 > How to detect blank input using Scanner?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to detect blank input using Scanner?
|
|
|
| I'm still a newbie in Java and I've searched the web for an answer but
have not been able to find any.
All I want is to do this:
- Ask user for file name to read
- if input is blank
assign a defaulted name
else
use the name the user gives.
As simple as this seems, I'm having trouble detecting blank input if
the user just hits <enter>.
My code:
....
Scanner input = new Scanner(System.ini);
// input.nextLine allows a carriage return
// input.next does not
String fileNameR = input.nextLine();
if (fileNameR == null) {
fileNameR = "grades.dat";
}
As long as I enter a word, this code works fine but if I hit return,
the 'if' statement is never true thus never executed. I've also
tried fileNameR == "\n" and fileNameR = "". All three conditions
don't work.
I've tried using charAt but my results become an exception because
this is an Index out of Bounds error.
(alternative charAt code I tried)
ch = fileNameR.charAt(0);
if (ch == '\n') {
... (code here)
}
Any help is appreciated.
Marion
| |
| Knute Johnson 2007-01-28, 7:10 pm |
| Taria wrote:
> I'm still a newbie in Java and I've searched the web for an answer but
> have not been able to find any.
>
> All I want is to do this:
>
> - Ask user for file name to read
> - if input is blank
> assign a defaulted name
> else
> use the name the user gives.
>
> As simple as this seems, I'm having trouble detecting blank input if
> the user just hits <enter>.
>
> My code:
> ...
> Scanner input = new Scanner(System.ini);
>
> // input.nextLine allows a carriage return
> // input.next does not
>
> String fileNameR = input.nextLine();
> if (fileNameR == null) {
> fileNameR = "grades.dat";
> }
>
> As long as I enter a word, this code works fine but if I hit return,
> the 'if' statement is never true thus never executed. I've also
> tried fileNameR == "\n" and fileNameR = "". All three conditions
> don't work.
>
> I've tried using charAt but my results become an exception because
> this is an Index out of Bounds error.
>
> (alternative charAt code I tried)
> ch = fileNameR.charAt(0);
> if (ch == '\n') {
> ... (code here)
> }
>
> Any help is appreciated.
> Marion
>
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
email s/nospam/knute/
| |
|
| > >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
| |
| Knute Johnson 2007-01-28, 7:10 pm |
| Taria wrote:
>
> 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/
| |
| Marion 2007-01-28, 7:10 pm |
| > 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/- Hide quoted text -- Show quoted text -
Whee! I looked at your code, tried not to flinch at the
BufferedReader code :p (Well, that stuff is totally alien to me.)
But from your code, it sparked a great idea of a solution, I
substituted "null" to "" and voila...it works with the Scanner
class.
Yihaa!
Y'know, I mulled over this small portion of the program longer than
the whole program itself. lol.
Thank you very much Knute, without your advice, I would have never
found this solution.
The elated newbie programmer. :)
| |
|
| Knute Johnson wrote:
Marion wrote:[color=darkred]
> 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/a...r.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/a...r.html#readLine()>
- Lew
|
|
|
|
|