How Can I Do This?

Started by
4 comments, last by Washu 14 years, 1 month ago
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;

    }

}
Advertisement
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"
inputvalue = dataIn.readLine();

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

}while ( inputvalue != 2);

// Type the exit code.

Would you mind implementing that for me? I'm very new to Java and most of this is extremely confusing to me.
Quote:Original post by 8bitrubix
Would 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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by 8bitrubix
Would 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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement