Solved

Try-Catch Problem

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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn different types of Android Layout and some basics of an Android App.

733 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