Solved

Try-Catch Problem

Posted on 2004-09-19
4
252 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
  • 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 100 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wordmultiple challenge 12 130
allswap challenge 6 96
Image decoding from Camera 3 85
This code tracks birthdays 3 72
A short article about a problem I had getting the GPS LocationListener working.
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

816 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now