Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

.showInputDialog()  RSS feed

 
Trey Cordova
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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).
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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 "".
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Will do, Maneesh.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!