Catching a HeadlessException when using JOptionPane for user input

jan_String = JOptionPane.showInputDialog("Enter a temperature for  January");

I want to iterate until the user inputs a number. I'm trying to catch an exception, but I can't seem to do that. The program (JApplet) still bombs, with a NumberFormatException in the Java console, when anything other than a number in entered. According to the Sun API, showInputDialog throws a HeadlessException. When I put the method in a try-catch, I still can't catch the exception when the input is not an integer.
HanRuiAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
Sorry - some errors there:


int temp = Integer.MIN_VALUE;
do {
      try {
            temp = Integer.parseInt(JOptionPane.showInputDialog("Enter a temperature for January"));
            System.out.println("Temperature is " + temp);
      }
      catch(NumberFormatException nfe) {
            // ignore it
      }
} while (temp == Integer.MIN_VALUE);

0
 
CEHJCommented:
Please post the code around that
0
 
CEHJCommented:
int temp = Integer.MIN_VALUE;
jan_String = JOptionPane.showInputDialog("Enter a temperature for January");
do {
      try {
            int temp = Integer.parseInt(java_String);
            System.out.println("Temperature is " + temp);
      }
      catch(NumberFormatException nfe) {
            // ignore it
      }
} while (temp == Integer.MIN_VALUE);
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
HanRuiAuthor Commented:
I also tried NumberFormatException in the catch.

while(try1){
 try {
 jan_String = JOptionPane.showInputDialog("Enter a temperature for  January");
 try1=false;
 }
 catch(HeadlessException he){
 System.out.println("Number format problem");
 }

}
0
 
CEHJCommented:
You're not parsing the input and are catching the wrong exception (see above)
0
 
HanRuiAuthor Commented:
Thanks. I'll have to check this later.
0
 
objectsCommented:
> You're not parsing the input and are catching the wrong exception (see above)

Doubt it from whats been said in the question.
0
 
HanRuiAuthor Commented:
It worked!! Thanks!
0
 
CEHJCommented:
:-)
0
All Courses

From novice to tech pro — start learning today.