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

• October 23rd, 2013, 01:15 PM
MLIAKIRA
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.

Attachment 2409

and here is my code

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; else{ break; }   }   } }}```
• October 23rd, 2013, 01:54 PM
GregBrannon
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.
• October 23rd, 2013, 02:21 PM
Norm
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?