0
101 Mar 07, 2010 at 21:21

How can I add a user input 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.util.Scanner;

public class Loan {

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

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

String inputvalue="";

System.out.println("Welcome to the Kentucky Savings Bank Loan Calculator\n");

while (!isNumeric(inputvalue)) {

System.out.println("What is the 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 do you wish to pay it in? ");

}

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;

}

}


#### 9 Replies

0
165 Mar 07, 2010 at 21:23

This is not a site for homework problems.

0
101 Mar 07, 2010 at 21:47

This is a personal project.

0
165 Mar 07, 2010 at 22:01

I don’t think I believe you. In any case, even if it’s not homework, it’s not game related so this still isn’t an appropriate site. :)

0
140 Mar 08, 2010 at 00:25

Well, it’s programming anyway. We’ll pretend it’s a game. Adding a user input loop for end or keep going is pretty easy. You just put a while loop around everything you want repeated and add another variable. Call it “done” or something.
bool done = false;
while(!done)
{
Do all this stuff.
if (user says yes) done = true
}

0
101 Mar 08, 2010 at 16:05

C’mon, you can’t google “java loops” so that you can finish your homework quickly? :)

Also, if it isn’t homework, I suggest you read a good Java intro book so that simple little things like this don’t trip you up. Would you operate a complex piece of machinery without opening a user’s manual?

0
101 Mar 08, 2010 at 17:07

@8bitrubix

Who said anything about homework? This is a personal project.

You write loan calculators in your spare time?

0
101 Mar 08, 2010 at 18:27

@poita

You write loan calculators in your spare time?

To each his own, I guess.

I once wrote an html editor, with syntax highlighting. That sounds like a horrible thing to do today, but it was quite fun at the time ;)

0
101 Mar 08, 2010 at 19:43

@poita

You write loan calculators in your spare time?

Well, in defense of OP, how many versions of Tetris are out there now? Two to five gazillion? :) ;)

0
101 Mar 08, 2010 at 22:18

@Kenneth Gorking

To each his own, I guess. I once wrote an html editor, with syntax highlighting. That sounds like a horrible thing to do today, but it was quite fun at the time ;)

That sounds awesome tbh. I’d take an HTML editor over a loan calculator any day.