# Try-Catch block issues

• 10-30-2013, 12:16 AM
Uchiha
Try-Catch block issues
So we have to make a mortgage calculation project where we have to ask the user to calculate again and now we have to make it so it prints out a error message every time the user enters a string value for any of the inputs. I thought I did it right but something strange happens every time I run it and I can't figure out why and I know it's something wrong with the Try-Catch blocks.
Here are my outputs:
http://i.imgur.com/cbvwM5v.png
As you can see the third time i run the program I enter a "two" as the second input and it still did the calculations. Then, the third time I tried it, I entered a negative number then a "two" and everything worked the way I wanted it to. Then, the last time I ran it I put a positive number for the first input and it still did the calculations, anything you guys see that might be doing this?
Here's my code:
Code:

```package MortgageCalculation2c; import java.text.NumberFormat; import java.util.Scanner; /**  *  * @author Akira  */ public class MortgageCalculation2c {     /**     * @param args the command line arguments     */     public static void main(String[] args) {         Scanner in = new Scanner(System.in);                         double loanAmount = 0;         double interestRate = 0;         double numberYears = 0;         double months;         double monthlyPayment;         double numerator;         double denominator;         double formula;         boolean userInput = true;         String answer = ("y");                         while (userInput) {             try {       //prompt user for the loan amount              System.out.print("Enter the loan amount: ");         loanAmount = Double.parseDouble(in.nextLine());         //prompt the user for the interest rate         System.out.print("Enter the rate: ");         interestRate = Double.parseDouble(in.nextLine());                 //prompt the user for  thenumber of years         System.out.print("Enter the number of years: ");         numberYears = Double.parseDouble(in.nextLine());                     } catch (NumberFormatException nfe) {                 System.out.println("You must enter positive numerical data!");             }                         //if the user enters a negative number print out a error message, if not, continue calculations       if ((loanAmount <= 0) || (interestRate <= 0) || (numberYears <= 0)) {             System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!");           } else {             //convert the interest rate           interestRate = interestRate / 100 / 12;                     //the number of years must be converted to months           months = numberYears * 12;           //numerator of the monthly payment formula           numerator = (Math.pow(1 + interestRate, months));                     //denominator of the monthly payment formula           denominator = ((numerator)-1);                     //the formula equals the numerator divided by the denominator           formula = ( numerator / denominator );                     //monthly payment calculation             monthlyPayment = (interestRate * loanAmount * formula);             //sytem output             NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();             System.out.println("The monthly payment is: " + defaultFormat.format(monthlyPayment));       }                   //prompt the user if they would like to calculate the program again.             System.out.println("Would you like to calculate again (y/n) : ");             //if the user enters "y" the program will run again and if the user enters anything else it ends                 answer = in.nextLine();             answer = answer.toLowerCase();                 if ( answer.equals("y")){                     userInput = true;  //tests the program if it needs to run again                 }else{                     break;  //ends the program                 }                               }     } }```
Is there anything that you guys can see that might be causing this problem?
• 10-30-2013, 01:39 AM
Uchiha
Re: Try-Catch block issues
Can nobody help me with this?
• 10-30-2013, 04:32 AM
foulkelore
Re: Try-Catch block issues
I don't think the problem is with your try-catch block. You are getting the NumberFormatException error message correctly, but the next if statement is still processing. Should it?
• 10-30-2013, 02:32 PM
Uchiha
Re: Try-Catch block issues
I'm not sure, that's why I'm confused...
• 10-30-2013, 03:26 PM
gimbal2
Re: Try-Catch block issues
Quote:

Originally Posted by Uchiha
I'm not sure, that's why I'm confused...

Bull. You are not confused, you do not want to think about it anymore. That needs to change, do whatever you need to do to get motivated again to work on this. When you do you'll find that it is pretty obvious what you should or should not do after the user inputs something that is not a number.