开发者

Question about the cancel button on a standard JOptionPane

So I am working on a program for school, and part of the assignment is to have a bunch of prompts for input pop up. I am using the JOptionPane, which inherently has an OK button and a Cancel button. Now, to make the program exit when they press cancel when the prompt is asking for a string, I have something like this:

firstName = JOptionPane.showInputDialog("Please enter your first name:");
if(firstName == null)System.exit(0);

But I also have to do the same thing for numbers I get as input, both Doubles and Ints. If I try the same thing, it throws an error saying The operator == is undefined for the argument type(s) double, null. So, what is the best way for me to check if they click Cancel when being prompted for a numerical value? Than开发者_如何学JAVAks for your help!

Edit #1

Here is the code for the JOptionPane getting a numerical value:

startDateMonth = Integer.parseInt(JOptionPane.showInputDialog("Please enter the start         date month (1-12):"));


JOptionPane.showInputDialog() returns always a string which is the user input. If the user has clicked the Cancel button it will return null. If you want to convert the user input to another type just parse the string. I mean the code you have pasted should remain the same. If you are asking for a different thing, please clarify.

Petar

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜