Jump to content


How Can I Do This?


9 replies to this topic

#1 8bitrubix

    New Member

  • Members
  • Pip
  • 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.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;

    }

}


#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 5311 posts
  • LocationSanta Clara, CA

Posted 07 March 2010 - 09:23 PM

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

#3 8bitrubix

    New Member

  • Members
  • Pip
  • 2 posts

Posted 07 March 2010 - 09:47 PM

Who said anything about homework?

This is a personal project.

#4 Reedbeta

    DevMaster Staff

  • Administrators
  • 5311 posts
  • LocationSanta Clara, CA

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

#5 fireside

    Senior Member

  • Members
  • PipPipPipPip
  • 1590 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.
ask user if done
if (user says yes) done = true
}
Currently using Blender and Unity.

#6 alphadog

    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.

#7 poita

    Senior Member

  • Members
  • PipPipPipPip
  • 322 posts

Posted 08 March 2010 - 05:07 PM

8bitrubix said:

Who said anything about homework?

This is a personal project.

You write loan calculators in your spare time?

#8 Kenneth Gorking

    Senior Member

  • Members
  • PipPipPipPip
  • 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

#9 alphadog

    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.

#10 poita

    Senior Member

  • Members
  • PipPipPipPip
  • 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.


alphadog said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users