Link to home
Start Free TrialLog in
Avatar of pgmtkl
pgmtkl

asked on

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;
                        }
Avatar of Manikandan Thiagarajan
Manikandan Thiagarajan
Flag of India image

close your public static void main method

closing bracket } required after catch block
Avatar of pgmtkl
pgmtkl

ASKER

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 {
                                ^
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;
                  }
ASKER CERTIFIED SOLUTION
Avatar of Kanti
Kanti

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pgmtkl

ASKER

Thanks. I added that and now that section is ok, no errors.