Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: What's wrong with my code!? seriously...

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Thanked 0 Times in 0 Posts

    Default What's wrong with my code!? seriously...

    Okay so basically I'm making a mortgage program and I'm supposed to ask the user if they would want to run the calculation again. It works for the most part, when i type in positive inputs, but when i enter a negative it doesn't ask the user. Also, there are lines under "y" and "userInput" that say the "variable is not used". How do i fix these problems, what's wrong with my code?! This program is due today so if comments can get straight to the point it would be greatly appreciated.

    here is a pic of me inputting numbers and notice how when i type a negative number it doesnt ask to run again.


    and here is my code

    package MortgageCalculation2b;
    import java.text.NumberFormat;
    import java.util.Scanner;
     * @author Akira
    public class MortgageCalculation2b {
         * @param args the command line arguments
        public static void main(String[] args) {
             Scanner in = new Scanner(System.in);
            double loanAmount;
            double interestRate;
            double numberYears;
            double months;
            double monthlyPayment;
            double numerator;
            double denominator;
            double formula;
            boolean userInput;
            String y;
            String answer;
            while (userInput = true) {
           //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());
            //the number of years must be converted to months
            months = numberYears * 12;
            //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;
               //numerator of the monthly payment formula
               numerator = Math.pow(1 + interestRate, months);
               //denominator of the monthly payment formula
               denominator = ((Math.pow(1 + interestRate, months))-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;

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 688 Times in 675 Posts

    Default Re: What's wrong with my code!? seriously...

    The "ask the user" part only occurs if valid numbers were entered, because that question is inside the else{} clause after the input validation if statement. That's how the program is designed. If you want to ask whether the user desires to continue even after invalid numbers have been entered, then move the question outside the else{} clause.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,320 Times in 2,276 Posts

    Default Re: What's wrong with my code!? seriously...

    Also a LONG thread for this topic at: How do I ask the user to do the calculation again?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. What's wrong with my code?
    By SecTech in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 23rd, 2013, 08:38 PM
  2. What Am I doing Wrong In This Code?
    By RadiusUnknown in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 14th, 2012, 04:00 PM
  3. What's wrong with my code?
    By mjballa in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 19th, 2011, 03:57 PM
  4. What is wrong with this code?
    By A19 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: August 11th, 2011, 10:45 PM
  5. What is wrong in the code
    By Rajiv in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: July 29th, 2011, 01:09 PM