문제

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? Thanks 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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top