| Author |
.showInputDialog()
|
Trey Cordova
Greenhorn
Joined: Sep 10, 2010
Posts: 3
|
|
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
Joined: May 03, 2008
Posts: 4163
|
|
|
That's not how to test for null.And next time, please use the code tags to post code.
|
luck, db
There are no new questions, but there may be new answers.
|
 |
Trey Cordova
Greenhorn
Joined: Sep 10, 2010
Posts: 3
|
|
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
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8430
|
|
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
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
Trey Cordova
Greenhorn
Joined: Sep 10, 2010
Posts: 3
|
|
|
Will do, Maneesh.
|
 |
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
|
|
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.
|
 |
 |
|
|
subject: .showInputDialog()
|
|
|