How can I add a loop to this so that it asks the user if they want to calculate another loan and if they choose no the program ends, but if they choose yes, the program loops back and asks for loan amount?

``````import java.io.BufferedReader;

import java.io.IOException;

public class Loan {

public static void main(String[] args) throws IOException {

double pay=0, interest=0, amount=0, years=0;

String inputvalue="";

while (!isNumeric(inputvalue)) {

System.out.println("Loan Amount? ");

}

amount = Float.parseFloat(inputvalue);

inputvalue="";

while (!isNumeric(inputvalue)) {

System.out.println("What is the Monthly Interest Rate? ");

}

interest = Float.parseFloat(inputvalue);

inputvalue="";

while (!isNumeric(inputvalue)) {

System.out.println("How many Years? ");

}

years = Float.parseFloat(inputvalue);

if (interest > 1) {

interest = interest / 100;

}

pay = (interest * amount / 12) /

(1.0 - Math.pow(((interest / 12) + 1.0), (-(12 * years))));

System.out.print("\nLoan Amount:\$" + amount);

System.out.print("\nInterest Rate:" + Round(interest * 100, 2) + "%");

System.out.print("\nNumber of Years:" + years + " (Months: " + years * 12 + ")");

System.out.print("\nThe Monthly Payment:\$" + Round(pay, 2));

}

private static boolean isNumeric(String str){

try {

Float.parseFloat(str);

return true;

} catch (NumberFormatException nfe){

return false;

}

}

public static float Round(double Rval, int Rpl) {

float p = (float)Math.pow(10, Rpl);

Rval = Rval * p;

float tmp = Math.round(Rval);

return (float)tmp/p;

}

}``````
2
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by stultuske

use nested loops, they work exactly the same as normal loops. here's a bit the logic to go:

while newInput = true{
inputNumber = ?
while not numerIc(inputNumber){
inputNumber = ?
}
processInputNumber
newInput = ? => true or false
}

Could someone put one into that code for me? I'm quite new to Java.

Could someone put one into that code for me? I'm quite new to Java.

``````while (condition){
while (condition2){

}
}``````

that's all there's to it ..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.