Need help with error message - Exception in thread "main" java.lang.ArrayIndexOutOfBo
This is for a class, and I already turned in my assignment. I'm getting the correct response from my script, but I'm getting the message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at mortgage.calculator.MortgageCalculator.main(Mortga geCalculator.java:40)
Java Result: 1
I've looked at examples, and understand that I've defined the parameters of something to be too small, but am so new to Java that I don't know where or how to fix it. I think this is indicating an issue in line 40?
Code:
/**
* PRG420 Week 2
* Katie Young
* SR-mf-003, Mortgage Payment Calculator
*/
package mortgage.calculator;
//The java.io package provides for system input and output through data streams
import java.io.*;
//A subclass of the NumberFormat used to format numbers in Java programs
import java.text.DecimalFormat;
public class MortgageCalculator
//The main function for the mortgage calculator
public static void main(String[] args) throws IOException
{
//Declaring and constructing variables, each field can be expanded to allow multiple variables
//Term of mortgage in years
int [] iTerm = {360};
//Interest rate
double [] dInterest = {5.25};
//Amount of loan
double dPayment, dRate, dAmount = 200000, dMonthlyInterest,dMonthlyPrincipal, dMonthlyBalance;
DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
//Loop for the varying Mortgage Rates and Payments
int p;
for (p = 0; p <= 2; p ++)
{
//Calculation for the monthly mortgage payment
//Calculations Retrieved from http://www.1728.com/loanform.htm on 8/15/08
[B]dRate = dInterest[p] / 1200;[/B]dPayment = (dAmount * dRate) / (1 - Math.pow(1 / (1 + dRate), iTerm[p]));
dMonthlyInterest = (dAmount / 12) * (dInterest[p] / 100);
dMonthlyPrincipal = (dPayment - dMonthlyInterest);
dMonthlyBalance = (dAmount - dMonthlyPrincipal);
int nbYear = iTerm[p] / 12;
System.out.println();
System.out.println("Your Monthly Payment for a " + nbYear +" year loan of "
+ "$200,000 at " + dInterest[p]+ "% is: " + twoDigits.format (dPayment));
System.out.println();
}
}
//function for the calculation of the interest into the loan payment
public double MonthlyInterest()
{
//Declaring Variables for interest on the loan
double dMonthlyInterest = 0.0;
double dAmount = 0.0;
double dInterest = 0.0;
//Calculation for monthly interest
dMonthlyInterest = (dAmount / 12) * (dInterest / 100);
return dMonthlyInterest;
}
//function for the calculation of the interest into the loan payment
public double monthlyInterest()
{
//Declaring Variables for interest on the loan
double dMonthlyInterest = 0.0;
double dAmount = 0.0;
double dInterest = 0.0;
//Calculation for monthly interest
dMonthlyInterest = (dAmount / 12) * (dInterest / 100);
return dMonthlyInterest;
}
//function of the calculation of the monthly principal for the loan payment
public static double monthlyPrincipal()
{
//Declaring Variables for monthly principal
double dMonthlyPrincipal = 0.0;
double dPayment = 0.0;
double dMonthlyInterest = 0.0;
//Calculations for monthly principal
dMonthlyPrincipal = (dPayment - dMonthlyInterest);
return dMonthlyPrincipal;
}
//function for the calculation of the monthy loan balance
public static double monthlyBalance()
{
//Declaring Variables for monthly loan balance
double dMonthlyBalance = 0.0;
double dAmount = 0.0;
double dMonthlyPrincipal = 0.0;
//Calculations for monthly loan balance
dMonthlyBalance = (dAmount - dMonthlyPrincipal);
return dMonthlyBalance;
}
}
I used the code symbols, but the line numbers went away when I edited. I bolded line 40.
Thank you,
Katie
Re: Need help with error message - Exception in thread "main" java.lang.ArrayIndexOut
Make sure p is above or equal to 0.
Also, you have only declared one value in dInterest. That means unless p == 0, you will get the exception.
Re: Need help with error message - Exception in thread "main" java.lang.ArrayIndexOut
I thought it was.
I don't think that Java programming is going to be my strong point in life. And just think this is only the first Java class. I still have a 2nd one to go!
Re: Need help with error message - Exception in thread "main" java.lang.ArrayIndexOut
It should work well as long as you use try-catch and by checking dynamic values that will error out if used incorrectly.
Remember to Rep too! (Press the little star)
Re: Need help with error message - Exception in thread "main" java.lang.ArrayIndexOut
Quote:
Originally Posted by
steamruler
It should work well as long as you use try-catch and by checking dynamic values that will error out if used incorrectly.
Remember to Rep too! (Press the little star)
If you want to mascerade an AIOOBE with a try ... catch block (and hide it?) you deserve to have you head nailed to the floor. No reps for that.
kind regards,
Jos
Re: Need help with error message - Exception in thread "main" java.lang.ArrayIndexOut
Quote:
Originally Posted by
JosAH
If you want to mascerade an AIOOBE with a try ... catch block (and hide it?) you deserve to have you head nailed to the floor. No reps for that.
kind regards,
Jos
Not to hide but rather stop the program and print useful stuff like p or anything too see what exactly made it error out. Here it was more of an incorrect use of arrays.
Re: Need help with error message - Exception in thread "main" java.lang.ArrayIndexOut
Quote:
Originally Posted by
steamruler
Not to hide but rather stop the program and print useful stuff like p or anything too see what exactly made it error out. Here it was more of an incorrect use of arrays.
If you just let the Exception pass you see everything you need to know from the Exception message and the stack trace.
kind regards,
Jos
Re: Need help with error message - Exception in thread "main" java.lang.ArrayIndexOut
Thanks for the help. I don't know enough yet for the answers you're giving to even make sense to me. I'll see if my teacher can help.
Re: Need help with error message - Exception in thread "main" java.lang.ArrayIndexOut
Thought about it some more and got out my calculator. P is less than 0. Because it is the 5.25% divided by 1200. Got it. I think.
Re: Need help with error message - Exception in thread "main" java.lang.ArrayIndexOut
You're receiving the error because your program is attempting to access the second and third elements of the dInterest array; however, these elements do not exist. When you declared the array on line 25 you substantiated it with a single double value. In Java, the size of an array is determined when it is substantiated, and cannot be changed while the program is running.
When the for loop (line 34) is executed the values of dInterest[p] are as follows:
dInterest[p] == 5.25 //p == 0
dInterest[p] == Error: does not exist. //p == 1
dInterest[p] == Error: does not exist. //p == 2
By substantiating it with 5.25, you limited the size of the array to a single element with the given value of 5.25. I can see you're trying to calculate the payment based on three different rates in the for loop. You could change the size of the array by adding additional values to line 25. Example:
double[] dInterest = {5.25, 6.25, 7.25};
Or you could set the size of the array and set the values afterwards:
double[] dInterest = new double[3];
dInterest[0] = 5.25;
dInterest[1] = 6.25;
dInterest[2] = 7.25;
(Sorry if my terminology is a little off; this is how I was told :P)
And don't worry about your mistakes; we all start somewhere :). I hope this helps.
Re: Need help with error message - Exception in thread "main" java.lang.ArrayIndexOut
I changed
Code:
//Loop for the varying Mortgage Rates and Payments
int p;
for (p = 0; p <= 2; p ++) {
to
Code:
//Loop for the varying Mortgage Rates and Payments
int p;
for (p = 0; p <= 0; p ++) {
Since p is equal (in this instance) to 0.004375
I kept thinking that it wanted the number of places involved and not the actual number that resulted from the calculation. Once I realized what it was really saying, I got it. Thank you, steamruler. It was your initial response that helped me get what I needed. I just kept reading it and finally had an ahah moment.