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;

import java.io.InputStreamReader;

public class Loan {

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

  
     	BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
     
        double pay=0, interest=0, amount=0, years=0;

        String inputvalue="";


        while (!isNumeric(inputvalue)) {

            System.out.println("Loan Amount? ");
			inputvalue = dataIn.readLine();
           

        }

        amount = Float.parseFloat(inputvalue);

        inputvalue="";

        while (!isNumeric(inputvalue)) {

            System.out.println("What is the Monthly Interest Rate? ");
			inputvalue = dataIn.readLine();
            

        }

        interest = Float.parseFloat(inputvalue);

        inputvalue="";

        while (!isNumeric(inputvalue)) {

            System.out.println("How many Years? ");
			inputvalue = dataIn.readLine();
           

        }

        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;

    }

}

Recommended Answers

All 3 Replies

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.

you allready have the code in your first post

while (condition){
  while (condition2){

  }
}

that's all there's to it ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.