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

error handling

I was updating my program to not accept character input by a user for a mortgage gui program. I have error handling for if the numeric amount is under or over an amount. My original catch expetion was "//catch(Exception e) { and i updated it to reflect   "catch(NumberFormatException ne){ . The program compiles but when i run it it just stops, it does not display the error message i had before with 'invalid entry'. Is the number format exception the wrong line of code to be using. Should i be using something different???

Here is my code:



import javax.swing.*;
import java.text.*; //DecimalFormat;
import java.math.*;
import java.lang.*;
import java.awt.*;



public class MortgageGui1
{
      //public static void main(String[] args)throws Exception

 public static void main(String[] args) throws Exception
 {
  int val = 0;
  String amount;
  String rate;
  String term;
  int response = 0;
  boolean valid = true;
 double termValue;
 double amountValue;
 double rateValue;
 //JFrame frame=new JFrame();


  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.
                        //JLabel lblAmount= new JLabel("Amount");
                        //frame.setTitle("Mortgage Calculator");
            amount = JOptionPane.showInputDialog(null,"Please enter amount to be financed:", " ", JOptionPane.QUESTION_MESSAGE);
            amountValue = Double.parseDouble(amount);


                        try {
                        
                                    // parseInt is a method of class Integer. parseInt converts a string into an integer value
                                    //val = Integer.parseInt(amount);
                                     
                                    if (amountValue < 0.0){
                                            JOptionPane.showMessageDialog(null, "Amount cannot be less than 0", "Error", JOptionPane.ERROR_MESSAGE);       
                                       continue;
                                    }
                                    else if (amountValue > 2000000.00) {
                                       JOptionPane.showMessageDialog(null, "Amount cannot exceed $2,000,000", "Error", JOptionPane.ERROR_MESSAGE);
                             continue;                  
                                    }
                                    }
                              
                        //      catch(Exception e) {
                              catch(NumberFormatException ne) {

                              // Error with the amount
                                    JOptionPane.showMessageDialog(null, "Error, invalid entry", "Error", JOptionPane.ERROR_MESSAGE);
                                    continue;
                              }      
                              
                        rate = JOptionPane.showInputDialog(null,"Please enter annual finance rate:", " ", JOptionPane.QUESTION_MESSAGE);
                  //      rateValue = Double.parseDouble(rate);
                  //      rateValue = Integer.parseInt(rate);
                        rateValue = Double.parseDouble(rate) / 100.0;

                        
                        try {
                              //      val = Integer.parseInt(rate);
                                    
                                    if (rateValue <0){
                                    JOptionPane.showMessageDialog(null, "Annual Finance rate cannot be less than 0", "Error", JOptionPane.ERROR_MESSAGE);       
                                       continue;
                                    }
                                    else if (rateValue > 14.0) {
                                       JOptionPane.showMessageDialog(null, "Annual Finance Rate cannot exceed 14%", "Error", JOptionPane.ERROR_MESSAGE);
                             continue;      
                                    }
                                    }
                                    //catch(Exception e) {
                          catch(NumberFormatException ne){
 
                                 // Error with the amount
                                    JOptionPane.showMessageDialog(null, "Error, invalid entry", "Error", JOptionPane.ERROR_MESSAGE);
                                    continue;
                              }
                              
                        term = JOptionPane.showInputDialog(null,"Please enter months/term of loan:", " ", JOptionPane.QUESTION_MESSAGE);
                        termValue = Double.parseDouble(term);


                        try {
                              //      val = Integer.parseInt(term);
                                    
                                    if (termValue <0){
                                    JOptionPane.showMessageDialog(null, "Months/Term cannot be less than 0", "Error", JOptionPane.ERROR_MESSAGE);       
                                       continue;
                                    }
                                    else if (termValue > 480) {
                                       JOptionPane.showMessageDialog(null, "Months/Term cannot exceed 480 months", "Error", JOptionPane.ERROR_MESSAGE);
                             continue;      
                        }
                        }
                                    //catch(Exception e) {
                                    catch(NumberFormatException ne) {

                              // Error with the amount
                                    JOptionPane.showMessageDialog(null, "Error, invalid entry", "Error", JOptionPane.ERROR_MESSAGE);
                                    continue;
                        
                        }
                        
                        //calculation of mortgage
                        //double rate -interest is entered by user above
                        //double amount- loan amount is entered by user above
                        //double intValue;
                        //intValue = rate/12; //*Monthy interest to be used in calculation
                        double J=rateValue/12;
                        //term - term of loan is entered by user above
                        DecimalFormat df = new DecimalFormat("$#,###.00");// to display amount in correct format
                        
                        //double mortgage = (Math.pow((1 + J), term) -1) / (J * Math.pow((1 + J), term));
                        //double payment = amount/ mortgage; //*Rate Calculation

double mortgage = (Math.pow((1 + J), termValue) - 1)
/ (J * Math.pow((1 + J), termValue));
double payment = amountValue / mortgage; //*Rate Calculation

String endAmount = "Your monthly mortgage payment is " + df.format(payment);

JOptionPane.showMessageDialog(null,endAmount,"Information",JOptionPane.INFORMATION_MESSAGE);
// JOptionPane.showMessageDialog(null,"Your monthly mortgage payment is " + payment,"Information",JOptionPane.INFORMATION_MESSAGE);

response = JOptionPane.showConfirmDialog(null,"Would you like to perform another calculation?",
"Try Again?",JOptionPane.YES_NO_OPTION);
}
while (response == 0);
}

}
// End of program

0
pgmtkl
Asked:
pgmtkl
  • 3
2 Solutions
 
Giant2Commented:
Move:
amountValue = Double.parseDouble(amount);
into the try catch
0
 
Giant2Commented:
even for:
 rateValue = Double.parseDouble(rate) / 100.0;
and
termValue = Double.parseDouble(term);

Hope this solves your problem.
Bye, Giant.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
If you want to accept only numbers and continue until a valid number is entered, you can do something like this:

do
{
  String v = JOptionPane.showInputDialog ( .... ) ;
  try
  {
    value = Double.parseDouble ( v ) ; // or Integer.parseInt ( v ) ; depending on what the data-type is
    break ; // break out of the loop
  }
  catch ( NumberFormatException e )
  {
  }
} while ( true ) ; // if an exception occurs, the break will not be called and this will re-loop
0
 
pgmtklAuthor Commented:
Thanks. Both of these ways worked, i decided to move the amount value, etc into the try statements. thanks
0
 
Giant2Commented:
Tx.
:-)

You can even do for example something like following:
String error="init";
while (!error.equals("")){
         error="";
         try {
             amountValue = Double.parseDouble(amount);
             if (amountValue < 0.0){
                          error="Amount cannot be less than 0";      
                              }
                              else if (amountValue > 2000000.00) {
                                 error="Amount cannot exceed $2,000,000";
                              }
           } catch(NumberFormatException ne) {
                 error"Error, invalid entry";
           }    
           if (!error.equals(""))JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE);
}

In this manner you centralize the way to display the error.

Bye, Giant.
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!

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