Solved

Try-Catch Problem

Posted on 2004-09-19
4
229 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
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 …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

759 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

19 Experts available now in Live!

Get 1:1 Help Now