Results 1 to 11 of 11
- 03-05-2012, 09:24 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 16
- Rep Power
- 0
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?
Java 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; } }
Thank you,
KatieLast edited by felonee; 03-05-2012 at 09:27 PM.
- 03-05-2012, 09:26 PM #2
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
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.Last edited by steamruler; 03-05-2012 at 09:30 PM.
- 03-05-2012, 09:30 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 16
- Rep Power
- 0
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!
- 03-05-2012, 09:34 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
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)
- 03-05-2012, 09:39 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Re: Need help with error message - Exception in thread "main" java.lang.ArrayIndexOut
Build a wall around Donald Trump; I'll pay for it.
- 03-05-2012, 09:46 PM #6
Member
- Join Date
- Feb 2012
- Posts
- 9
- Rep Power
- 0
- 03-05-2012, 09:50 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Re: Need help with error message - Exception in thread "main" java.lang.ArrayIndexOut
Build a wall around Donald Trump; I'll pay for it.
- 03-05-2012, 09:57 PM #8
Member
- Join Date
- Mar 2012
- Posts
- 16
- Rep Power
- 0
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.
- 03-05-2012, 10:51 PM #9
Member
- Join Date
- Mar 2012
- Posts
- 16
- Rep Power
- 0
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.
- 03-05-2012, 10:53 PM #10
Member
- Join Date
- Mar 2012
- Posts
- 9
- Rep Power
- 0
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.Last edited by Jeff_H; 03-05-2012 at 11:00 PM.
- 03-05-2012, 10:57 PM #11
Member
- Join Date
- Mar 2012
- Posts
- 16
- Rep Power
- 0
Re: Need help with error message - Exception in thread "main" java.lang.ArrayIndexOut
I changed
Java Code://Loop for the varying Mortgage Rates and Payments int p; for (p = 0; p <= 2; p ++) {
Java Code://Loop for the varying Mortgage Rates and Payments int p; for (p = 0; p <= 0; p ++) {
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.
Similar Threads
-
Error:Exception in thread "main" java.lang.NoSuchMethodError:main
By jerry jessie in forum New To JavaReplies: 6Last Post: 01-21-2012, 06:59 AM -
Question about error "Exception in thread "main" java.lang.NoSuchMethodError: main
By ferdzz in forum New To JavaReplies: 5Last Post: 06-22-2010, 04:51 PM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 08:58 PM -
ERROR: Exception in thread "main" java.lang.NoSuchMethodError: main
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 08:10 AM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 11:55 PM
Bookmarks