• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

try/catch

I am working on this section of code below to get the try and catch to run properly in a gui program. The program ran fine when i had one section and then i add the rate and term and receive an error that i need try with catch, which i have. Am i missing something or is the order wrong? thanks


 public static void main(String[] args) throws Exception
 {
  int val = 0;
  String amount;
  String rate;
  String term;
  int response = 0;
  boolean valid = true;

  do{    
            // The showInoutDialog is a method of JOptionPane and its return value is a string.
                        // Since the return value is a string, it is necessary that "grade" should be of a string
                        // datatype too.
                        
            amount = JOptionPane.showInputDialog(null,"Please enter amount to be financed:", " ", JOptionPane.QUESTION_MESSAGE);
           
                        try {
                        
                                    // parseInt is a method of class Integer. parseInt converts a string into an integer value
                                    val = Integer.parseInt(amount);
                                     
                                    if (val < 0){
                                            JOptionPane.showMessageDialog(null, "Amount cannot be less than 0", "Error", JOptionPane.ERROR_MESSAGE);       
                                       continue;
                                    }
                                    else if (val > 2000000) {
                                       JOptionPane.showMessageDialog(null, "Amount cannot exceed $2,000,000", "Error", JOptionPane.ERROR_MESSAGE);
                             continue;                  
                                    }
                              
                              catch(Exception e) {
                              // Error with the amount
                                    JOptionPane.showMessageDialog(null, "Error, invalid entry", "Error", JOptionPane.ERROR_MESSAGE);
                                    continue;
                                    }
                              
                        rate = JOptionPane.showInputDialog(null,"Please enter finance rate:", " ", JoptionPane.QUESTION_MESSAGE);
                        
                        try {
                                    val = Integer.parseInt(rate);
                                    
                                    if (val <0){
                                    JOptionPane.showMessageDialog(null, "Finance rate cannot be less than 0", "Error", JOptionPane.ERROR_MESSAGE);       
                                       continue;
                                    }
                                    else if (val > 14) {
                                       JOptionPane.showMessageDialog(null, "Finance Rate cannot exceed 14%", "Error", JOptionPane.ERROR_MESSAGE);
                             continue;      
                                    }
                                    catch(Exception e) {
                              // Error with the amount
                                    JOptionPane.showMessageDialog(null, "Error, invalid entry", "Error", JOptionPane.ERROR_MESSAGE);
                                    continue;
                              }
                              
                        term = JOptionPane.showInputDialog(null,"Please enter term:", " ", JoptionPane.QUESTION_MESSAGE);
                        
                        try {
                                    val = Integer.parseInt(term);
                                    
                                    if (val <0){
                                    JOptionPane.showMessageDialog(null, "Term cannot be less than 0", "Error", JOptionPane.ERROR_MESSAGE);       
                                       continue;
                                    }
                                    else if (val > 480) {
                                       JOptionPane.showMessageDialog(null, "Finance Rate cannot exceed 480 months", "Error", JOptionPane.ERROR_MESSAGE);
                             continue;      
                  
                        }
                                    catch(Exception e) {
                              // Error with the amount
                                    JOptionPane.showMessageDialog(null, "Error, invalid entry", "Error", JOptionPane.ERROR_MESSAGE);
                                    continue;
                        }
0
pgmtkl
Asked:
pgmtkl
  • 2
  • 2
1 Solution
 
Manikandan ThiagarajanSenior consultantCommented:
close your public static void main method

closing bracket } required after catch block
0
 
pgmtklAuthor Commented:
After each of the 3 catch sections? I added that and still get
'catch' without 'try'
                              catch(Exception e) {
                                        ^
MortgageGui1.java:24: 'try' without 'catch' or 'finally'
                        try {
                                ^
0
 
KantiCommented:
Try with this code

       public static void main(String[] args) throws Exception
       {
        int val = 0;
        String amount;
        String rate;
        String term;
        int response = 0;
        boolean valid = true;

        do{    
                  // The showInoutDialog is a method of JOptionPane and its return value is a string.
                  // Since the return value is a string, it is necessary that "grade" should be of a string
                  // datatype too.
                          
                  amount = JOptionPane.showInputDialog(null,"Please enter amount to be financed:", " ", JOptionPane.QUESTION_MESSAGE);
                  
                   try
                        {
                          // parseInt is a method of class Integer. parseInt converts a string into an integer value
                          val = Integer.parseInt(amount);
                                      
                          if (val < 0)
                                {
                                      JOptionPane.showMessageDialog(null, "Amount cannot be less than 0", "Error", JOptionPane.ERROR_MESSAGE);      
                                      continue;
                                }
                          else if (val > 2000000)
                                {
                                      JOptionPane.showMessageDialog(null, "Amount cannot exceed $2,000,000", "Error", JOptionPane.ERROR_MESSAGE);
                                      continue;              
                                }
                        }          
                  catch(Exception e)
                        {
                        // Error with the amount
                      JOptionPane.showMessageDialog(null, "Error, invalid entry", "Error", JOptionPane.ERROR_MESSAGE);
                      continue;
                  }
                              
                  rate = JOptionPane.showInputDialog(null,"Please enter finance rate:", " ", JoptionPane.QUESTION_MESSAGE);
                          
                  try
                        {
                          val = Integer.parseInt(rate);
                                    
                          if (val <0)
                                {
                                      JOptionPane.showMessageDialog(null, "Finance rate cannot be less than 0", "Error", JOptionPane.ERROR_MESSAGE);      
                                      continue;
                                }
                          else if (val > 14)
                                {
                                      JOptionPane.showMessageDialog(null, "Finance Rate cannot exceed 14%", "Error", JOptionPane.ERROR_MESSAGE);
                                      continue;    

                 }
                   catch(Exception e)
                        {
                         // Error with the amount
                         JOptionPane.showMessageDialog(null, "Error, invalid entry", "Error", JOptionPane.ERROR_MESSAGE);
                         continue;
                  }
                              
                   term = JOptionPane.showInputDialog(null,"Please enter term:", " ", JoptionPane.QUESTION_MESSAGE);
                          
                  try
                        {
                          val = Integer.parseInt(term);
                                    
                          if (val <0)
                          {
                                JOptionPane.showMessageDialog(null, "Term cannot be less than 0", "Error", JOptionPane.ERROR_MESSAGE);      
                                continue;
                          }
                          else if (val > 480)
                          {
                                JOptionPane.showMessageDialog(null, "Finance Rate cannot exceed 480 months", "Error", JOptionPane.ERROR_MESSAGE);
                                continue;    
                          }
                        }
                  catch(Exception e)
                        {
                         // Error with the amount
                          JOptionPane.showMessageDialog(null, "Error, invalid entry", "Error", JOptionPane.ERROR_MESSAGE);
                          continue;
                  }
0
 
KantiCommented:
You have a do there just check for it

do {
    statement(s)
} while (expression);


and for the main you might have to add one more closing } at the end afte the above code
0
 
pgmtklAuthor Commented:
Thanks. I added that and now that section is ok, no errors.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now