Here is what the program needs to be able to do:
//Run1
"Enter loan amount: 100000
Enter rate: 6
Enter number years: 30
The monthly payment is: $599.55
Would you like to calculate again (y/n): y"
// Run 2
Enter loan amount: -110000
Enter rate: 5
Enter number years: 6
You must enter positive numeric data!
Would you like to calculate again (y/n): n
- If the user inputs a negative number the program tells you to enter positive numeric data.
- If the user inputs "y" then the program goes back to the beginning if they enter "n" then the program exits.
- I have to use the in.nextLine() method , and the String.equals(string2) method .
I do not quite understand the loop statements. I have tried a while loop and do loop. The if statement inside the loop and outside the loop. I'm lost on this. Can someone point me in the right direction.
How do I write the loop?
package assignment2b; import java.util.Scanner; public class Assignment2b { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner in = new Scanner(System.in); // input object double loanAmount; // user input double interestRate; // user input double yearNumber; // user input double monthlyPayment; // payment from math.pow double finalPayment; // actual payment displayed double monthNumber; // number of months double monthlyRate; // interest rate by month System.out.print("Enter loan amount: "); // Line to input loan amount loanAmount = Double.parseDouble (in.nextLine()); // Gets input number System.out.print("Enter rate: "); // Line to input rate amount interestRate = Double.parseDouble (in.nextLine()); // Gets input number monthlyRate = interestRate /100 /12; //Convert rate into decimals System.out.print("Enter number years: "); // Line to input number of years yearNumber = Double.parseDouble (in.nextLine()); // Gets input number monthNumber = yearNumber * 12; // Total amount of months monthlyPayment = Math.pow(1+monthlyRate , monthNumber) / (Math.pow(1+monthlyRate, monthNumber)-1); finalPayment = (loanAmount*monthlyRate*monthlyPayment); // Gives the payment if (loanAmount <= 0 || interestRate <=0 || yearNumber <=0) // incorrect# line { System.out.println("\nYou must enter positive numeric data!"); System.out.println("\nWould you like to calculate again (y/n): "); in.nextLine(); } else // correct# line { System.out.printf("\nThe monthly payment is: $%.2f", finalPayment); // Monthly payment System.out.println(); System.out.println("\nWould you like to calculate again (y/n): "); in.nextLine(); System.out.println(); // Moves build line } } }