Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

showInputDialog

Thread view: 
Eitan M - 04 Apr 2006 08:36 GMT
Hello,
I have use in my program :
s = JOptionPane.showInputDialog( ....)

when I press just OK,
s should be "",
but if I ask in my program :
if (s=="") ...
the result of if statment is false.

Also I cannot catch case when the user press "Cancel",

So What I did is :
if (s == null || s.length() = 0)
instead of
if (s == 0)

why (s==0) doesn't work properly ?
And what should I do in my code ?

Thanks :)
Thomas Weidenfeller - 04 Apr 2006 10:01 GMT
> when I press just OK,
> s should be "",
> but if I ask in my program :
> if (s=="") ...
> the result of if statment is false.

You want to dig out a Java text book and learn about how to do proper
string comparison in Java. You are not comparing string contents in your
code, you are just comparing string references. You would have to be
lucky if these two references indeed point to the same string.

> why (s==0) doesn't work properly ?

Again, get out a text book about Java. This is basic Java syntax. A
string is not an integer, and there is no such thing as an implicit type
conversion between String and int. If you want such stuff, try C or C++.
There you can compare some integer value with a pointers to some memory
area which might or might not hold a string.

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Eitan M - 04 Apr 2006 12:36 GMT
Sorry,
for my question, I have ment :
> why (s=="") doesn't work properly ?

instead of
> why (s==0) doesn't work properly ?

(s=="") returns false, even I just press OK button.

Why ?
Gregor Kovač - 04 Apr 2006 12:45 GMT
> Sorry,
> for my question, I have ment :
[quoted text clipped - 6 lines]
>
> Why ?
Maybe because == compares refereces and not values ?
Do (s.compareTo("") == 0)
Signature

-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~

| Gregor Kovac | Gregor.Kovac@mikropis.si    |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| In A World Without Fences Who Needs Gates? |
|            Experience Linux.               |
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
Luc The Perverse - 04 Apr 2006 12:48 GMT
> Sorry,
> for my question, I have ment :
[quoted text clipped - 6 lines]
>
> Why ?

I'm not sure you listened to Thomas.

But s is an object (I'm presuming of type String) and "" is an object.  The
== operator will only tell you if they are the same object.

Try ("".equals(s))

--
LTP

:)
Roedy Green - 04 Apr 2006 15:40 GMT
On Tue, 4 Apr 2006 10:24:21 +0200, "Eitan M"
<no_spam_please@nospam_please.com> wrote, quoted or indirectly quoted
someone who said :

>if (s=="") ...
>the result of if statment is false.
[quoted text clipped - 5 lines]
>instead of
>if (s == 0)

see http://mindprod.com/jgloss/gotchas.html#COMPARISON
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.