How Can I Do This?

810abd9d2a60344404d6bbadf252df83
0
8bitrubix 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.io.InputStreamReader;

import java.util.Scanner;


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="";

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

        while (!isNumeric(inputvalue)) {

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

    }

}

9 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Mar 07, 2010 at 21:23

This is not a site for homework problems.

810abd9d2a60344404d6bbadf252df83
0
8bitrubix 101 Mar 07, 2010 at 21:47

Who said anything about homework?

This is a personal project.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 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. :)

A638aa42130293f319eda7fa4ba121f4
0
fireside 141 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.
ask user if done
if (user says yes) done = true
}

8676d29610e6c98d6dd2d9c38528cd9c
0
alphadog 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?

36b416ed76cbaff49c8f6b7511458883
0
poita 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?

46407cc1bdfbd2db4f6e8876d74f990a
0
Kenneth_Gorking 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 ;)

8676d29610e6c98d6dd2d9c38528cd9c
0
alphadog 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? :) ;)

36b416ed76cbaff49c8f6b7511458883
0
poita 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.

@alphadog

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

I dunno… given the average introductory programming course, I’m pretty sure there’s more loan calculators and simple bank simulators than Tetris clones.