Solved

Try-Catch Problem

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A short article about problems I had with the new location API and permissions in Marshmallow
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

707 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