Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-03-2005, 03:41 AM   #1 (permalink)
rockybalboa
Registered User
 
Join Date: Mar 2005
Posts: 6
rockybalboa is on a distinguished road
Help formatting array outputs

Hi,
I am hoping someone can help me get this cleaned up.
My calculations are working fine but I can't seem to get the format right.
I would like to break down each loan display by year and month, ie.
Yr 1
Month
1
2
3
.
.
6
Yr2
Month
blah
blah
and so on.
One of my problems has been that it is taking the term data from the array. I was able to get this to work fine when the term value was hard coded, but I haven't been able to get this part figured out since I started taking the value assignments from the arrays.
Before the array implementation I had it set up to display a year and it's 12 months, paused, and then started again with the next year.
Anyway, I'd appreciate any advice anyone could offer.
Thank you,
Dan

P.S. I am currently getting each payment listed one line after another without any break. Also the only val I am currently getting in the month column is the total months -1.

Code:
import java.text.*;
import java.util.*;
import java.lang.*;
import java.io.*;

public class MCalcv5
{  
    public static void main(String[] args) 
    {
        double monthlyPayment;//monthly payment calculation (includes interest)
        double []principal={200000,200000,200000};//principal amount, same for all three loans
        double []interestRateYears={5.35,5.5,5.75};//annual interest rate
        int []termYears={7,15,30};//length of loan in years
        double interestPaid;//accumulator for total interest over whole loan
        double interest;//monthly interest payment
        double monthlyPrincipalPayment;//monthly payment to principal only
        double totalPaid;//accumulator for total payments over whole loan
        
        for (int i=0;i<principal.length; i++)//loop to run until pricipal has expired
        {
            double loan=principal[i];//set var loan equal to val in array
            double prt=interestRateYears[i];//set var prt equal to val in array
            double interestRateMonths = (interestRateYears[i] / 1200);//calculate monthly interest rate from annual
            int termMonths = (termYears[i] * 12);//calculate months from years
            monthlyPrincipalPayment = 0;//monthly payment excluding interest
            double balance = principal[i];//set balance from principal
            interest = 0;
            interestPaid = 0;
            totalPaid = 0;
            int x = termYears[i];
            {
                for (int y = 0; y < x; y++);//loop to calculate display monthly values
                {
                    DecimalFormat dec = new DecimalFormat(",   ###.00  ");//needed to output currency
                    termMonths--;
                    System.out.println("\n\n\t*** Mortgage Calculator***\n\n"+//display column headings
                            "\nLoan   "+(i+1)+//
                            "\nLoan Amount: \t$" + dec.format(principal[i]) +//
                            "\nInterest Rate: \t" + interestRateYears[i] +"%" +//
                            "\nTerm (years): \t" + termYears[i]);//
                    
                    monthlyPayment = (loan * interestRateMonths)/
                            (1-Math.pow(1+ interestRateMonths, - termMonths));//calculate monthly payment
                    
                    System.out.println("\n Monthly payment = " + "$" + dec.format (monthlyPayment));
                    System.out.println("\n\nMonths\tPrincipal Paid\t\tInterest Paid\t\tBalance");
                    System.out.println("------\t---------\t\t--------\t\t-------\t");
                    {
                        for(int k = 0; k<=termMonths; k++)
                        {
                            interest= balance * interestRateMonths;                         //formula for monthly intrest
                            interestPaid=interestPaid+interest;                             //totals interest paid
                            monthlyPrincipalPayment = monthlyPayment-interest;              //subtracts interest from principal
                            balance= balance - monthlyPrincipalPayment;                     //subtracts principal from balance
                            totalPaid= interestPaid + principal[i];            //displays total interest and original loan balance
                            
                            System.out.println(termMonths + "\t$"+dec.format(monthlyPrincipalPayment)+"\t\t$"+
                                    dec.format(interest) +"\t\t$"+ dec.format(balance));
                        }
                        try                                                 //for display delay
                        {
                            Thread.sleep(200);                              //
                        }
                        catch (InterruptedException e)                      //
                        {
                        }
                    }
                    System.out.println("\n Total Interest Paid = " + "$" + dec.format (interestPaid));  //total interest paid
                    System.out.println("\n Total Amount Paid ="+"$"+ dec.format (totalPaid));           //total amount paid
                }
            }
        }
    }
}
rockybalboa is offline   Reply With Quote
Old 04-03-2005, 06:48 AM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
You have a lot of frivilous brackets that you should get rid of. Also, you might want to read the Java Coding Conventions link in the Java Resources sticky.

Now, I understand what the first for loop is doing; cycling through all the term lengths. I understand what the second for loop is doing; going through all the years in any particular term. I don't understand what the third for loop is doing, however. If you want to go through all the months, why not simply have it go 12 months? It's not like any of the terms go for a fraction of a year. As it is now, you seem to have that particular loop go for 12 * the number of years the term is for. That would mean that your first year of a 7 year loan would be 84 months long.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting an array of any Comparable objects plague Java 1 03-14-2005 07:24 PM
Simple reverse programm silex Standard C, C++ 3 01-22-2005 09:03 AM
working with Array Goong MS Technologies ( ASP, VB, C#, .NET ) 3 07-22-2004 01:14 PM
breaking down an array from a form metazai PHP 12 07-09-2004 07:18 AM
adding to an array in php sde PHP 1 06-12-2002 12:04 PM


All times are GMT -8. The time now is 01:22 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting