Help with Java Program

Loan Payment Program

Page 1 of 1

3 Replies - 420 Views - Last Post: 04 November 2010 - 06:19 AM Rate Topic: -----

#1 etrac1912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-November 10

Help with Java Program

Posted 04 November 2010 - 05:22 AM

I'm trying to see if someone can help me with two issues with my program. It compiles fine without errors, but some of my numbers in my amortization table only gives me one number after the decimal place and others give me two numbers. I was wondering if something in my code is incorrect where it keeps giving me only one number when I am asking for two. I attached the output screen and below is my code so you can see what I am talking about. The second issue is I would like to add a comma between the correct numbers to represent, thousands. For example, the answer on the total payment is $11739.68. How can I get it to say $11,739.68? Any help would be great.

Here is the class that holds all the math.

package loanapplication;

import javax.swing.table.DefaultTableModel;


public class Model {
      
   public static final String[] COLUMN_NAMES = {
      "Payment", "Interest", "Principal", "Balance"
   };

    int loanAmount;
    int years;
    double a_i_r;
    double monthlyPymnt;
    double monthlyRate;
    double totalPymnt;
    double[] interest;
    double[] principal;
    double[] balance;
    double remaining_principle;
    int for_dec=1000;


    public boolean initialization(String loanAmountText, String numberOfYearsText, String AIRText, int dec_places) {

        try
        {
            loanAmount = Integer.parseInt(loanAmountText);
            years= Integer.parseInt(numberOfYearsText);
            a_i_r=Double.parseDouble(AIRText);
            for_dec=1;
            for(int j=0; j<dec_places; j++)
            {
                for_dec=for_dec*10;
            }

            return true;
        }
        catch(Exception e)
        {
            return false;

        }
    }

    //calculating Monthly Payment
    public String getMonthlyPayment()
    {
        monthlyRate=a_i_r/1200;
        monthlyPymnt = (loanAmount * (monthlyRate+(monthlyRate/((Math.pow((1+monthlyRate), years*12))-1))));
        return "$"+Math.floor(monthlyPymnt*for_dec)/for_dec+"";
    }

    //calculating Total Payment
    public String getTotalPayment()
    {
       totalPymnt= monthlyPymnt*years*12;
            return "$"+ Math.floor(totalPymnt * for_dec)/for_dec;
    }

    //calculating interest, Principal, Balance value for each month
    public DefaultTableModel get_table()
    {
       DefaultTableModel table = new DefaultTableModel(COLUMN_NAMES, 0);
            interest = new double[years*12];
            principal = new double[years*12];
            balance = new double[years*12];

            remaining_principle= loanAmount;
            principal[0]=remaining_principle;

            for(int i=0; i<years*12; i++)
            {
                interest[i]=remaining_principle* monthlyRate;
                principal[i]=monthlyPymnt -interest[i];
                remaining_principle=remaining_principle-principal[i];

                balance[i]=remaining_principle;
                Object[] row = new Object[4];

                row[0]=i+1;
                row[1]="$"+Math.floor(interest[i]*for_dec)/for_dec;
                row[2]="$"+Math.floor(principal[i]*for_dec)/for_dec;
                row[3]="$"+Math.floor(balance[i]*for_dec)/for_dec;
                table.addRow(row);
            }
            return table;
    }
}


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

Attached image(s)

  • Attached Image

This post has been edited by JackOfAllTrades: 04 November 2010 - 05:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with Java Program

#2 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: Help with Java Program

Posted 04 November 2010 - 05:42 AM

Looks like a string formatting issue and not a digit issue. I'd use:
DecimalFormat df = new DecimalFormat("#.##");
row[0]=i+1;
row[1]="$"+df.format(Math.floor(interest[i]*for_dec)/for_dec);
row[2]="$"+df.format(Math.floor(principal[i]*for_dec)/for_dec);
row[3]="$"+df.format(Math.floor(balance[i]*for_dec)/for_dec);



Should ensure your digits all have 2 decimal places even when the last digit is 0.

EDIT: Prettified.

This post has been edited by SpeedisaVirus: 04 November 2010 - 05:50 AM

Was This Post Helpful? 1
  • +
  • -

#3 etrac1912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-November 10

Re: Help with Java Program

Posted 04 November 2010 - 06:01 AM

I works PERFECTLY now!! Thanks for your help!!
Was This Post Helpful? 0
  • +
  • -

#4 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: Help with Java Program

Posted 04 November 2010 - 06:19 AM

Noooooo problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1