?
Solved

JOptionPane return Value

Posted on 2003-03-11
12
Medium Priority
?
275 Views
Last Modified: 2010-03-31
Ok this may sound like a strange question but if a user clicks on OK in a JOptionPain dialog box and they have not entered anything into the field then what does it store in the string.

I use:

//store input into value
String value = JOptionPane.showInputDialog( getContentPane(), "text","title", QUESTION_MESSAGE );

//make sure the user entered a value
if ((value == null) || (value == ""))
   return; // aborts the operation

//...otherwise continue

yet if i just click on OK and dont enter any text the program 'continues' and does not 'return'

so what does value equal???

thanks,
   MTRUBS
0
Comment
Question by:MTRUBS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 8112986
Try

if ((value == null) || (value.trim().equals("")))
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8113007
It sends you the empty string, ""
0
 

Author Comment

by:MTRUBS
ID: 8113010
ah..

thank you very much that worked wonderfully :)
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:MTRUBS
ID: 8113027
and no

if(value == "") //does not work
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8113043
that's because when you compare strings, you must use, string1.equals(string2) NOT the "=="
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8113052
That's right. Save to trim as well or user could have entered space
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8113055
>>Save to trim

should have been

>>Safer to trim

!
0
 

Author Comment

by:MTRUBS
ID: 8113074
oh i see....thanks, both of you
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8113099
Sorry - should have explained earlier
0
 

Author Comment

by:MTRUBS
ID: 8113136
so is there a better way to write:

if(value == null)

or does it have to be done that way since value.equals(null) throws a NullPointerException?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8113165
>>or does it have to be done that way

Yes. For the reason you gave - there's no object to call a method on.
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8113174
no, if value could be a nullpointer you do "value == null" or uses try - catch

But here, the value is the empty string, not a null, so we don't need the nullpointer check!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month13 days, 5 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question