JOptionPane return Value

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
MTRUBSAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
Try

if ((value == null) || (value.trim().equals("")))
0
 
antonsigurCommented:
It sends you the empty string, ""
0
 
MTRUBSAuthor Commented:
ah..

thank you very much that worked wonderfully :)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
MTRUBSAuthor Commented:
and no

if(value == "") //does not work
0
 
antonsigurCommented:
that's because when you compare strings, you must use, string1.equals(string2) NOT the "=="
0
 
CEHJCommented:
That's right. Save to trim as well or user could have entered space
0
 
CEHJCommented:
>>Save to trim

should have been

>>Safer to trim

!
0
 
MTRUBSAuthor Commented:
oh i see....thanks, both of you
0
 
CEHJCommented:
Sorry - should have explained earlier
0
 
MTRUBSAuthor Commented:
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
 
CEHJCommented:
>>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
 
antonsigurCommented:
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
All Courses

From novice to tech pro — start learning today.