Results 1 to 5 of 5
  1. #1
    Uchiha is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default 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:

    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:
    Java 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?

  2. #2
    Uchiha is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Re: Try-Catch block issues

    Can nobody help me with this?

  3. #3
    foulkelore is offline Member
    Join Date
    Dec 2012
    Location
    Des Moines, IA
    Posts
    35
    Rep Power
    0

    Default 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?

  4. #4
    Uchiha is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Re: Try-Catch block issues

    I'm not sure, that's why I'm confused...

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,358
    Rep Power
    6

    Default Re: Try-Catch block issues

    Quote Originally Posted by Uchiha View Post
    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.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Try Catch Block
    By Jackbower in forum New To Java
    Replies: 3
    Last Post: 05-06-2012, 08:00 AM
  2. Try/catch block
    By swati.jyoti in forum New To Java
    Replies: 5
    Last Post: 07-02-2009, 03:32 PM
  3. Question reg try/catch block
    By nn12 in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 06:56 PM
  4. Try Catch block issues
    By kewlgeye in forum New To Java
    Replies: 11
    Last Post: 04-29-2008, 08:10 AM
  5. try...catch block
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 08:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •