What does "Enter" return?

I am trying to allow the user to just press "Enter" to return to the main menu of my class. But when I test it in the if statement it never gives me true. What am I doing wrong?

Here is the code section :

String ext =null;

System.out.print("Please enter extension or Enter to return to Main menu: ");

ext = (kr.readLine());

;

if (!ext.equalsIgnoreCase(null) ){System.out.println("I'm in here");

fb.currentMask =(ext);

fb.getFiles(ext);

}

So far I have tried testing ext against null, "" and " " .

Also I've tried using equals, equalsIgnoreCase, and ==.

[831 byte] By [Palmera] at [2007-10-2 6:50:07]
# 1
test against "", not null
tjacobs01a at 2007-7-16 13:59:03 > top of Java-index,Java Essentials,New To Java...
# 2

whatever.equals(null) (including equalsIgnoreCase(null)) can never be true in Java.

Null and the empty string ("") are not the same.

String s1 = null; // s1 is a reference that doesn't point to any object

String s2 = ""; // s2 is a reference that points to a String object that has no characters

System.out.println(s1 == null); // true

System.out.println(s2 == null); // false

System.out.pirntln(s1.equals(null)); // NullPointerException

System.out.println(s2.equals(null)); // false

System.out.pirntln(s1.equals(s2)); // NullPointerException

System.out.println(s2.equals(s1)); // false

jverda at 2007-7-16 13:59:03 > top of Java-index,Java Essentials,New To Java...
# 3

> test against "", not null

I tried the following:

String ext = null;

System.out.print("Please enter extension or Enter to return to Main menu: ");

ext = (kr.readLine());

;

if (!ext.equalsIgnoreCase("") ){System.out.println("I'm in here");

fb.currentMask =(ext);

fb.getFiles(ext);

}

If this looks right, is there something else that would cause the "if" to never return truel.

Palmera at 2007-7-16 13:59:03 > top of Java-index,Java Essentials,New To Java...
# 4

We don't actually know what the "kr" variable is. If it's a BufferedReader then it would trim off line-ending characters. But maybe it's something else that leaves the line-ending characters alone, and you're getting them.

Why don't you just look at the contents of ext instead of inviting us to speculate? If you don't have a debugger thenSystem.out.println(ext);

System.out.println(ext.length());

System.out.println("" + ext + "");

are all good choices. Guessing is a slow process.

DrClapa at 2007-7-16 13:59:03 > top of Java-index,Java Essentials,New To Java...
# 5
seems like another option would beext.length()>0
walker8a at 2007-7-16 13:59:04 > top of Java-index,Java Essentials,New To Java...
# 6

> We don't actually know what the "kr" variable is. If

> it's a BufferedReader then it would trim off

> line-ending characters. But maybe it's something else

> that leaves the line-ending characters alone, and

> you're getting them.

>

> Why don't you just look at the contents of ext

> instead of inviting us to speculate? If you don't

> have a debugger thenSystem.out.println(ext);

> System.out.println(ext.length());

> System.out.println("" + ext + "");

are

> all good choices. Guessing is a slow process.

I put in the code you suggested and found that when Enter was pressed then ext was printing out the line:

Please enter extension or Enter to return to Main menu:

I have been using this KeyboardReader our instructor gave us and when I opened it up to look, it is a bufferedReader.

I can fix the problem if I use a println before the readLine instead of using print before the readLine. I don't know why this works. The readLine works when anything but "" is entered by the user. Is there a way to use the print statement?

Palmera at 2007-7-16 13:59:04 > top of Java-index,Java Essentials,New To Java...