# How Can I Do This?

This topic is 3145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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;

}

}


##### Share on other sites
It looks like you don't have a variable to test the condition. It seems like as long as it's not an numeric character, the code will keep executing. What you want to do is give the user a choice. Ask them if they want to proceed, if their answer is no, then go to the exit code. If it's yes, then enter the loop.
I am also going to recomend you put all that code in a function as well.
1. Get the input
2. based on the input decide whether to proceed.

I am not sure about the java specific implementation, but generally speaking you should probably enclose that code in a while loop.

inputvalue = 0;

do
{
//loan code
System.out.println("Welcome to the Kentucky Savings Bank Loan Calculator\n");
System.out.println("Please choose from the following options. Press 1 to calcutlate and 2 to exit"

if(inputvalue = 1)
{
// Put loan code here. I would recommend using a function.
}

}while ( inputvalue != 2);

// Type the exit code.

##### Share on other sites
Would you mind implementing that for me? I'm very new to Java and most of this is extremely confusing to me.

##### Share on other sites
Quote:
 Original post by 8bitrubixWould you mind implementing that for me? I'm very new to Java and most of this is extremely confusing to me.
This looks an awful lot like a homework problem, and we aren't allowed to help with homework.

Consider using [source][/source] tags to post large quantities of source code. Unlike [code] tags, source tags will use a scrolling div.

##### Share on other sites
Quote:
 Original post by 8bitrubixWould you mind implementing that for me? I'm very new to Java and most of this is extremely confusing to me.

If we would implement it for you, you wouldn't have learned anything, so we would actually be hurting you. Instead, try this recommended reading.

Homework.

##### Share on other sites

This topic is 3145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

This topic is now closed to further replies.

1. 1
2. 2
3. 3
4. 4
Rutin
19
5. 5

• 14
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632927
• Total Posts
3009254
• ### Who's Online (See full list)

There are no registered users currently online

×