Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

.showInputDialog()

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I am having a small problem and I am sure it is something easy to figure out.

I need to convert the null value that results from the user clicking "cancel" to a blank statement or "".

I have this code and something goes wrong to the point where I can't check for null values:

inputString = javax.swing.JOptionPane.showInputDialog(null, "Input any text.");

if (inputString.equals("null") {

inputString = "";

}

The statement works fine, but if the user decides to input "null" into the text-box the if-statement still runs as true. I understand why it runs as true. All I need to know is how to distinguish between the two (if possible).
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not how to test for null.And next time, please use the code tags to post code.
 
Trey Cordova
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I am being a tiny bit confusing. I have used that condition before. The problem here is that when the value is saved in "inputString" it saves as "null". This would be fine, but the method that I have created is tearing apart the user's string that he/she put in and evaluating it. When the value is null (user clicks cancel) I need it to send the 'inputString = ""' through the method. But, if the user, him/herself, enters "null" in the input box, my if-statement isn't able to tell the difference.

Here is the code:



In the end, if the user clicked "cancel" the result with the condition that you suggested returns "null" and it evaluates the word null. I need it to evaluate "".
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trey,
Welcome to the Ranch.

Your question would suit better on the UI forums. Moving thread.
Please do take out time to go through http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch
 
Trey Cordova
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will do, Maneesh.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Trey Cordova wrote:I think I am being a tiny bit confusing. I have used that condition before. The problem here is that when the value is saved in "inputString" it saves as "null". This would be fine, but the method that I have created is tearing apart the user's string that he/she put in and evaluating it. When the value is null (user clicks cancel) I need it to send the 'inputString = ""' through the method. But, if the user, him/herself, enters "null" in the input box, my if-statement isn't able to tell the difference.


Yes, your if statement will easily tell the difference. Darryl isn't suggesting to test


but rather

and there's a big difference between the two. His suggestion will work just fine for you if implemented.
 
reply
    Bookmark Topic Watch Topic
  • New Topic