?
Solved

Catching a HeadlessException when using JOptionPane for user input

Posted on 2005-03-17
9
Medium Priority
?
550 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:HanRui
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13567971
Please post the code around that
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13568027
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
 

Author Comment

by:HanRui
ID: 13568046
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 13568062
You're not parsing the input and are catching the wrong exception (see above)
0
 

Author Comment

by:HanRui
ID: 13568099
Thanks. I'll have to check this later.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 13568279
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
 
LVL 92

Expert Comment

by:objects
ID: 13569062
> You're not parsing the input and are catching the wrong exception (see above)

Doubt it from whats been said in the question.
0
 

Author Comment

by:HanRui
ID: 13569063
It worked!! Thanks!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13570626
:-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question