Welcome to Dream.In.Code
Become a Java Expert!

Join 149,587 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,783 people online right now. Registration is fast and FREE... Join Now!




Java mortgage calculator

 
Reply to this topicStart new topic

Java mortgage calculator, decimal errors and looping problem

leeboy
8 Oct, 2007 - 12:52 PM
Post #1

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 1


My Contributions
I have done pretty good with out any assistance but I am done to my last night and I am stumped.. The good news is that I have it working... however, I need to be able to input a decimal for the annual interest input, if that is input it freaks out and quits, if a whole number is entered it works fine.

The only other issue I need to fix, it that I need to print the Monthly Payment. I can make it print but I don't want it in the loop, because it prints over and over obviously. I can make it print to the screen anywhere else! Help!


CODE

import java.io.*;
import java.text.*;


public class MortgageCalculatorWK4{


private static BufferedReader stdin = new BufferedReader(
    new InputStreamReader (System.in));


public static void main(String[] args) throws IOException{
  //local objects
  MortgageCalculatorWK4 mortgageCalc = new MortgageCalculatorWK4();
  DecimalFormat df = new DecimalFormat("###,##0.00"); // to format the amount.
  DecimalFormat numDf = new DecimalFormat("000"); // to format the sequence number.

double principal = 0;
double annualInterest = 0.0;
int numOfYears = 0;

System.out.print("Please enter the amount borrowed:"); //prompt user input
String input = stdin.readLine(); //get user input
principal= Integer.parseInt(input); //convert string to int

System.out.print("Please enter the interest rate:");
String input2 = stdin.readLine();
  annualInterest = Integer.parseInt(input2);

System.out.print("Please enter the term of the loan in years:");
String input3 = stdin.readLine();
  numOfYears = Integer.parseInt(input3);

  //variables
  double monthlyInterest = annualInterest/(12*100);
  int totalNumOfMonths = numOfYears*12;


   System.out.println("Payment#   LoanBalance    InterestPaid");
   int monthCounter=1;
   for(; totalNumOfMonths>0; totalNumOfMonths--){
       //first get the monthly payment...
         double monthlyPayment = mortgageCalc.getMortgageAmount(principal,monthlyInterest,totalNumOfMonths);

      //now get the monthly principal in the payment...
         double monthlyPrincipal = mortgageCalc.getMonthlyPrincipal(monthlyPayment,monthlyInterest,principal);
         principal-=monthlyPrincipal;

         System.out.println(numDf.format(monthCounter) + "          " +df.format(principal)+ "          " +df.format((monthlyPayment-monthlyPrincipal)));
         monthCounter++;
         if(monthCounter%25==0)sleep(1000); // this will hesitate...


    }

}

public double getMortgageAmount(double principal, double monthlyInterest, int numOfMonths){
        // Monthly payment formula: M = P x (J/(1-(1+J)^-N));
        // M = P * ( J / (1 - (1 + J) ** -N));
       // source: http://www.hughchou.org/calc/formula.html
       double monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1))))));
       return monthlyPayment;

     }

//calculate the monthly interst using simple interest.
public double getMonthlyPrincipal(double monthlyPayment, double monthlyInterest, double remainingPrincipal){
       return monthlyPayment - (remainingPrincipal * monthlyInterest);
}

// method to make the program sleep (hesitate) for a give time (in milliseconds)..
public static void sleep(long milliseconds){
     try{
          Thread.sleep(milliseconds);
     }catch(Exception e){
         //do nothing... for now...
     }
}

}


Any help would be greatly appreciated biggrin.gif
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Java Mortgage Calculator
8 Oct, 2007 - 10:12 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Yeah no problem here... where you parse the loan amount and the interest rate you use Integer.parseInt(input) but your variables are doubles. See any problem here? When you enter a double it goes crazy because you are trying to parse it into an int and store it into a double. You need to parse it as a double since your variables and data is of type double.

So here is how you would collect your loan amount...

CODE

System.out.print("Please enter the amount borrowed:"); //prompt user input
String input = stdin.readLine(); //get user input

// Parse it to DOUBLE since your variable principal is a double datatype
// Using integer drops your decimal and can cause your problem.
principal = Double.parseDouble(input);


As you can see in the code above we parse the input as a double and store it in your double data variable principal. Do the same with your interest rate collection and you will see there isn't a problem.

Now remember if someone enters anything but a whole number of double (such as a sentence) you will still have problems because you have no checks. But as far as handling decimals, you should be good to go.

Enjoy!

"At DIC we be coding ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:03PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month