# How Can I Do This?

9 replies to this topic

### #18bitrubix

New Member

• Members
• 2 posts

Posted 07 March 2010 - 09:21 PM

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;

}

}


### #2Reedbeta

DevMaster Staff

• 5307 posts
• LocationBellevue, WA

Posted 07 March 2010 - 09:23 PM

This is not a site for homework problems.
reedbeta.com - developer blog, OpenGL demos, and other projects

### #38bitrubix

New Member

• Members
• 2 posts

Posted 07 March 2010 - 09:47 PM

This is a personal project.

### #4Reedbeta

DevMaster Staff

• 5307 posts
• LocationBellevue, WA

Posted 07 March 2010 - 10:01 PM

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.
reedbeta.com - developer blog, OpenGL demos, and other projects

### #5fireside

Senior Member

• Members
• 1587 posts

Posted 08 March 2010 - 12:25 AM

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
}
Currently using Blender and Unity.

DevMaster Staff

• Moderators
• 1716 posts

Posted 08 March 2010 - 04:05 PM

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?
Hyperbole is, like, the absolute best, most wonderful thing ever! However, you'd be an idiot to not think dogmatism is always bad.

### #7poita

Senior Member

• Members
• 322 posts

Posted 08 March 2010 - 05:07 PM

8bitrubix said:

This is a personal project.

You write loan calculators in your spare time?

### #8Kenneth Gorking

Senior Member

• Members
• 939 posts

Posted 08 March 2010 - 06:27 PM

poita said:

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 ;)
"Stupid bug! You go squish now!!" - Homer Simpson

DevMaster Staff

• Moderators
• 1716 posts

Posted 08 March 2010 - 07:43 PM

poita said:

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?
Hyperbole is, like, the absolute best, most wonderful thing ever! However, you'd be an idiot to not think dogmatism is always bad.

### #10poita

Senior Member

• Members
• 322 posts

Posted 08 March 2010 - 10:18 PM

Kenneth Gorking said:

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.