?
Solved

Try-Catch Problem

Posted on 2004-09-19
4
Medium Priority
?
281 Views
Last Modified: 2010-04-17
I am trying to do a try catch concerning the entering of letters.  When i enter a letter I display the message that says, you must enter a number, however my problem is that the user is not returned to the dialog box to re-enter the number.  It skips to the next dialog box.  I believe that I may have my try-catch in the wrong position or am missing one line.  Any assitance is appreciated.

BJ

  do {                
             
            valid=false;
                assignment1 = JOptionPane.showInputDialog(null, "Please Enter The Assigment Grade For The Student" );                        
                             
            try {
               
           
                assignment1S[i] = Double.parseDouble( assignment1 );
               
            } catch(NumberFormatException e){    
                     
             JOptionPane.showMessageDialog( null, "You did not enter a number, please try again.");              
               
             valid=true;
             
            }
                                       
               if ((assignment1S[i] < 0) || (assignment1S[i] > 100))
                   
                   JOptionPane.showMessageDialog( null, "Please enter a number between 0 and 100.");                  
                   
               
         
            } while ((assignment1S[i] < 0) || (assignment1S[i] > 100));
0
Comment
Question by:brenoby
[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
  • 2
4 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 12096060
Just reset focus back to the control in question.
0
 
LVL 19

Accepted Solution

by:
mrwad99 earned 400 total points
ID: 12096281
I think that a possible answer has been given by cookre above.  However, if the dialogue is destroyed after inputting, then I think this will do the trick:

do {                
             
            valid = false;

            assignment1 = JOptionPane.showInputDialog(null, "Please Enter The Assigment Grade For The Student" );                                      
            try {
                  assignment1S[i] = Double.parseDouble( assignment1 );
               
            } catch(NumberFormatException e){    
                     
                   JOptionPane.showMessageDialog( null, "You did not enter a number, please try again.");              
               
                   valid = false;
                   continue; // jump to start of loop
            }
                                       
            if ((assignment1S[i] < 0) || (assignment1S[i] > 100)) {      
                JOptionPane.showMessageDialog( null, "Please enter a number between 0 and 100.");                  
                valid = false;  
            } else {
            valid = true;
            }
         
} while (!valid);
0
 

Author Comment

by:brenoby
ID: 12113670
Thanks for the assistance.  I see where I made my error.

BJ
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 12114825
Glad to help.
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Introduction to Processes
Suggested Courses

741 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