JOptionPane

Hello, I am using a JOptionPane like this:

String name = JOptionPane.showInputDialog(new Frame(),"Enter name","New name", JOptionPane.PLAIN_MESSAGE);

And I want the user valid a string name, i use that:

if (name != null) System.out.println("name:"+name); //ok button

else System.out.println("no name:"); //cancel button

cancel is normal but ok is not :

If I press ok without enter a name, the result is "name: ",

I want to really valid only if the user enter a name.

Could someone help me please, thanks.

[575 byte] By [cece3] at [2007-9-26 6:33:12]
# 1

You need to check that name != "", or you could check the length of name.Either one would work. I personally use:

if(name == null || name == "") {

JOptionPane.showMessageDialog(this, "Invalid name entered. Please try again.", "Enter Name", 1);

m

wywiwyg at 2007-7-1 15:44:30 > top of Java-index,Archived Forums,Swing...
# 2

> You need to check that name != "", or you could check

> the length of name.Either one would work. I

> personally use:

> > if(name == null || name == "") {

> JOptionPane.showMessageDialog(this, "Invalid name

> e entered. Please try again.", "Enter Name", 1);

>

>

> m

This won't work either. The statement 'name == ""' is comparing the pointers, not the strings themselves. You need a 'name.equals("")'

schneidh at 2007-7-1 15:44:31 > top of Java-index,Archived Forums,Swing...
# 3

Actually, it will work...

Code:

String a = "";

String b = "";

System.out.println("a == b --> " + ( a == b ) );

System.out.println("a.equals(b) --> " + a.equals(b) );

Output:

a == b --> true

a.equals(b) --> true

Since Strings are immutable, Java gives both variables the same pointer, since they are both pointing to the same immutable String.

For myself, I use the "a.length() <= 0" test, instead of the ' a=="" ' test.

Regards,

Adrian

aromanelli at 2007-7-1 15:44:31 > top of Java-index,Archived Forums,Swing...
# 4
Sorry, you are correct. I had a temporary brainfart...m
wywiwyg at 2007-7-1 15:44:31 > top of Java-index,Archived Forums,Swing...
# 5
No problem. The immutable String topic is one that I get drain bamage over all the time! :)
aromanelli at 2007-7-1 15:44:31 > top of Java-index,Archived Forums,Swing...
# 6

> Actually, it will work...

>

>

> Code:

>

> String a = "";

> String b = "";

> System.out.println("a == b --> " + ( a == b ) );

> System.out.println("a.equals(b) --> " + a.equals(b)

> );

>

>

> Output:

>

> a == b --> true

> a.equals(b) --> true

>

>

> Since Strings are immutable, Java gives both variables

> the same pointer, since they are both pointing to the

> same immutable String.

>

> For myself, I use the "a.length() <= 0" test, instead

> of the ' a=="" ' test.

>

> Regards,

>

> Adrian

Actually, it may or may not work.

string1.equals(string2) does NOT always mean string1 == string2, System.out.println("" == "");

System.out.println(new String("") == new String(""));

will give you true and false.

So it's better to always use equals() to compare two Strings, or use length() in your case.

chuanhaochiu at 2007-7-1 15:44:31 > top of Java-index,Archived Forums,Swing...
# 7

Yeah, you make a good point, since you are using the String constructor, the two sides of the equal test will not be the same, since there will be two objects constructed and stored in two different memory locations with the same text. It only works with static (right term?) Strings like "", which are stored just once in memory by Java and then its reference location is reused over and over again.

And as you said, its always better using the ".equals()" method, and not having to worry about all of this! :)

The original point to all this was to check for an empty String. For what its worth, here's what I use:

if ( myString == null || myString.length() <= 0 ) { ...

I have no idea if its faster to do it that way, vs. ...

if ( myString == null || myString.equals("") ) { ...

I've always been uncomfortable using the "" String, and prefer the .length() check. Your mileage may vary.

Regards,

Adrian

aromanelli at 2007-7-1 15:44:31 > top of Java-index,Archived Forums,Swing...
# 8
Thanks for your answers. It works well.
cece3 at 2007-7-1 15:44:31 > top of Java-index,Archived Forums,Swing...