Help! Simple Java Program.

Here is the problem: Write a program that creates a loan amortization table. The user of the program will supply values for Initial Loan Principal, Annual Percentage Rate and Monthly Payment. The program should print out the appropriate amortization table including the number of Monthly Payments and the Total Interest paid for the life of the loan. The program should allow multiple

runs (up to 4 different versions) of the table process and should allow the

user to compare runs in a tabular format.

This is what I have so far and should work one I completely debug it. I am really new to Java, and I thinks it is somthing to do with the Array. I recently switched from C++ to Java, so I don't think I have the syntax down correctly. Thanks.

import javax.swing.JOptionPane;

publicclass Program_01

{

publicstaticvoid main(String [] args)

{

int Runs;

do{

String Run_Question = JOptionPane.showInputDialog(null,"How many times,would you like to run" +

" the program? (Up to four runs only!)","Run Question", JOptionPane.QUESTION_MESSAGE);

Runs = Integer.parseInt(Run_Question);

if( Runs < 0 || Runs > 4)

{

JOptionPane.showMessageDialog(null,"The number is not valid, try again!","Error",

JOptionPane.INFORMATION_MESSAGE);

}

}while(Runs < 0 || Runs > 4);

Questions(Runs);

}

publicstaticvoid Questions(int Runs)

{

for(int index = 0;index < Runs;index ++)

{

String Loan_Principle_String = JOptionPane.showInputDialog(null,"What is the Loan Principle?","Loan Amount",

JOptionPane.QUESTION_MESSAGE);

String Percentage_Rate_String = JOptionPane.showInputDialog(null,"What is the Annual Percentage Rate?",

"Interest Rate", JOptionPane.QUESTION_MESSAGE);

String Monthly_Payment_String = JOptionPane.showInputDialog(null,"What is the Monthly Payment","Payment",

JOptionPane.QUESTION_MESSAGE);

double Loan_Principle = Double.parseDouble(Loan_Principle_String);

double Percentage_Rate = Double.parseDouble(Percentage_Rate_String);

double Monthly_Payment = Double.parseDouble(Monthly_Payment_String);

Calculate(Loan_Principle, Percentage_Rate, Monthly_Payment);

}

}

publicstaticvoid Calculate (double Loan,double Percentage,double Monthly)

{

double[] Principle =newdouble[]{0,0};

double[] Interest =newdouble[]{0,0};

double[] Remain =newdouble[]{0,0};

double Monthly_Percent = Percentage/12;

double MPR = ((int)((Monthly_Percent + .005) * 100.) / 100.);

boolean Over =true;

double Borrowed = Loan;

int Month = 0;

for (int index = 0; Over !=false; index++)

{

double Rate = MPR * Loan;

double decimalRate = ((int)((Rate + .005) * 100.) / 100.);

Interest[index] = decimalRate;

double Prince = Monthly - decimalRate;

double decimalPrince = ((int)((Prince + .005) * 100.) / 100.);

Principle[index]= decimalPrince;

double Rmn = Borrowed - decimalPrince;

Borrowed = Rmn;

double decimalRmn = ((int)((Rmn + .005) * 100.) / 100.);

Remain[index] = decimalRmn;

if(Monthly > Remain[index] && Remain[index] > 0)

{

Monthly = Remain[index];

Over =false;

}

Month += 1;

}

System.out.println("I am here!" + Month);

System.out.println("Payment #" +" " +"Principle" +" " +"Payment" +" " +"APR" +" " +"MPR" +

" " +"Interest Payment" +" " +"Principle Payment");

System.out.println("");

for(int index = 0; index < Month; index++)

{

System.out.println((index + 1) +"\t" + Remain[index] +"\t" + Percentage +"\t" + MPR +"\t" + Interest[index]

+"\t" + Principle[index]);

}

}

}

[7222 byte] By [SlickWilly440a] at [2007-10-2 10:49:12]
# 1
And what is your actual question? What is going wrong or stops you from completing the program?Mike
bellyrippera at 2007-7-13 3:06:14 > top of Java-index,Java Essentials,Java Programming...
# 2

Array Index Out Of Bounds Exception on the following line

Interest[index] = decimalRate;

To sort of patch this I did the following....

changed the for loop to be

for (int index = 0; index<Interest.length; index++)

and the part where you have the value set to false I instead did...

if(Monthly > Remain[index] && Remain[index] > 0)

{

Monthly = Remain[index];

break;

}

Your code is really hard to follow with all the nonstandard variable naming schemes and various complexities.

When I did that it sort of works... but it only prints out two months. I am not sure what it is supposed to do. It looks to me like the logic needs work. At any rate you were blowing up because you were trying to access non existant array elements.

bellyrippera at 2007-7-13 3:06:14 > top of Java-index,Java Essentials,Java Programming...
# 3

I am trying to get an output like this.

Payment # Principal Payment APRMPRInterest Payment Principal Payment

11000.00100.00 12.00 0.0110.00 90.00

2910.00100.00 12.00 0.01 9.10 90.90

3819.10100.00 12.00 0.01 8.19 91.81

4727.29100.00 12.00 0.01 7.27 92.73

5634.56100.00 12.00 0.01 6.35 93.65

6540.91100.00 12.00 0.01 5.41 94.59

7446.32100.00 12.00 0.01 4.46 95.54

8350.78100.00 12.00 0.01 3.51 96.49

9254.29100.00 12.00 0.01 2.54 97.46

10156.83100.00 12.00 0.01 1.57 98.43

1158.4058.98 12.00 0.01 0.58 58.40

Total Interest Paid58.98

SlickWilly440a at 2007-7-13 3:06:14 > top of Java-index,Java Essentials,Java Programming...
# 4

> I am trying to get an output like this.

>

> Payment # Principal Payment APRMPRInterest

> Payment Principal Payment

>

> 11000.00100.00 12.00 0.01

> 0.0110.00 90.00

> 2910.00100.00 12.00 0.01

> 0.01 9.10 90.90

> 3819.10100.00 12.00 0.01

> 0.01 8.19 91.81

> 4727.29100.00 12.00 0.01

> 0.01 7.27 92.73

> 5634.56100.00 12.00 0.01

> 0.01 6.35 93.65

> 6540.91100.00 12.00 0.01

> 0.01 5.41 94.59

> 7446.32100.00 12.00 0.01

> 0.01 4.46 95.54

> 8350.78100.00 12.00 0.01

> 0.01 3.51 96.49

> 9254.29100.00 12.00 0.01

> 0.01 2.54 97.46

> 10156.83100.00 12.00 0.01

> .01 1.57 98.43

> 1158.4058.98 12.00 0.01

> .01 0.58 58.40

>

>Total Interest Paid58.98

Well that might be a tad optimistic. All your double arrays only have 2 slots instead of 11 or 12 or whatever number you do want so that isn't going to happen until you make them have those based on the rest of your code.

SlickWilly440a at 2007-7-13 3:06:14 > top of Java-index,Java Essentials,Java Programming...
# 5

Okay, here is my updated code. I am having trouble outputting my formate to align with each category.

import javax.swing.JOptionPane;

import java.text.DecimalFormat;

public class Program_01

{

public static void main(String [] args)

{

int Runs;

do

{

String Run_Question = JOptionPane.showInputDialog(null, "How many times would you like to run" +

" the program? (Up to four runs only!)", "Run Question", JOptionPane.QUESTION_MESSAGE);

Runs = Integer.parseInt(Run_Question);

if( Runs < 0 || Runs > 4)

{

JOptionPane.showMessageDialog(null, "The number is not valid, try again!", "Error",

JOptionPane.INFORMATION_MESSAGE);

}

}while(Runs < 0 || Runs > 4);

Questions(Runs);

}

public static void Questions(int Runs)

{

for(int index = 0;index < Runs;index ++)

{

String Loan_Principle_String = JOptionPane.showInputDialog(null, "What is the Loan Principle?", "Loan Amount",

JOptionPane.QUESTION_MESSAGE);

String Percentage_Rate_String = JOptionPane.showInputDialog(null, "What is the Annual Percentage Rate?",

"Interest Rate", JOptionPane.QUESTION_MESSAGE);

String Monthly_Payment_String = JOptionPane.showInputDialog(null, "What is the Monthly Payment", "Payment",

JOptionPane.QUESTION_MESSAGE);

double Loan_Principle = Double.parseDouble(Loan_Principle_String);

double Percentage_Rate = Double.parseDouble(Percentage_Rate_String);

double Monthly_Payment = Double.parseDouble(Monthly_Payment_String);

Calculate(Loan_Principle, Percentage_Rate, Monthly_Payment);

}

}

public static void Calculate (double Loan, double Percentage, double Monthly)

{

double[] Principle = new double[100];

double[] Interest = new double[100];

double[] Remain = new double[100];

double[] Payment = new double[100];

DecimalFormat Decimal = new DecimalFormat(".00");

double Total_Interest = 0;

double T_Interest;

double Monthly_Percent = (Percentage/12)/100;

double MPR = Monthly_Percent;

boolean Over = true;

double Borrowed = Loan;

double Deposit = Monthly;

int Month = 0;

for (int index = 0; Over = true; index++)

{

double Rate = MPR * Borrowed;

double decimalRate = round(Rate,2);

Interest[index] = decimalRate;

Remain[index] = Borrowed;

Month += 1;

if(Monthly > Remain[index ] && Remain[index ] > 0)

{

for(int count = 0; count <= Month; count++)

{

Total_Interest += Interest[count];

}

T_Interest = round(Total_Interest,2);

Payment[index] = T_Interest;

Deposit = T_Interest;

}

else

{

Payment[index] = Deposit;

}

double Prince = Deposit - decimalRate;

double decimalPrince = round(Prince,3);

Principle[index]= decimalPrince;

System.out.println("I am here!" + decimalPrince);

double Rmn = Borrowed - decimalPrince;

double decimalRmn = round(Rmn,2);

Borrowed = decimalRmn;

if(Monthly > Remain[index ] && Remain[index ] > 0)

{

Over = false;

break;

}

}

System.out.println("Payment #" + " " + "Principle" + " " + "Payment" + " " + "Percentage" + " " + "MPR" +

" " + "Interest Payment" + " " + "Principle Payment");

System.out.println("-");

for(int index = 0; index < Month; index++)

{

System.out.println("" + (index + 1) + "\t" + Decimal.format(Remain[index]) + "" +

Decimal.format(Payment[index]) + "" + Decimal.format(Percentage) + "" + MPR +

"\t" + Decimal.format(Interest[index]) + "\t\t" + Decimal.format(Principle[index]));

}

T_Interest = ((int)((Total_Interest + .005) * 100.) / 100.);

System.out.println("\n\t\t\t" + "Total Interest Paid \t" + T_Interest + "\n");

for(int index = 0; index < Month; index++)

{

System.out.printf("%f %f\n", Principle[index],Remain[index]);

}

}

public static double round(double val, int places) {

long factor = (long)Math.pow(10,places);

// Shift the decimal the correct number of places

// to the right.

val = val * factor;

// Round to the nearest integer.

long tmp = Math.round(val);

// Shift the decimal the correct number of places

// back to the left.

return (double)tmp / factor;

}

}

SlickWilly440a at 2007-7-13 3:06:14 > top of Java-index,Java Essentials,Java Programming...
# 6

> Okay, here is my updated code. I am having trouble

> outputting my formate to align with each category.

Then you need to print a new line after every time you perform your calculations.

Something like this:

Calculate(Loan_Principle, Percentage_Rate, Monthly_Payment);

System.out.println(); // print a new line

And in your Calculate(...) method replace the line: System.out.println("I am here!" + decimalPrince);

with the following line: // this will keep all column on the same line,

// separated with tabs

System.out.print(decimalPrince+"\t");

And please go over these tutorials:

Object-Oriented Programming Concepts

http://java.sun.com/docs/books/tutorial/java/concepts/

Java Naming Conventions

http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html

Both of these things, you apparently never heard of.

; )

prometheuzza at 2007-7-13 3:06:14 > top of Java-index,Java Essentials,Java Programming...