0

not sure how this site works...

I'm stuck with a run error (Array out of bound exception 4)and can't figure it out, i'm new to the world of Java programming, so I'm hoping I'm not asking anything dump!

pasted my code here:

/* Purpose: To develop a program to accept multiple inputs from loan information on
            three different loans(Loan Amount, Interest Rate and Loan Period). Calculate the
            necessary elements such as the Months and Monthly payments and output the loan amount,
            interest rate, period length and monthly payment.*/

import java.math.*;
import java.util.*;
import java.io.*;
import java.text.*;

public class mortgageIA4
{
    public static void main(String[] args) throws IOException
    {

//Declaring variables

String loan,interestRate,period;
double interest, payment;
int looq,q;
interest = 0.00;
payment =0.00;
q=0;
DecimalFormat decimalPlaces=new DecimalFormat("0.00");

//For accepting values from keyboard
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));




//Array assignments

double interest1 [] = new double [3];
int term1 [] = new int[3];
double mPayment[] = new double[4];
double loan1 [] = new double[3];
//Ask user for inputs
//Start questions loop

for (looq = 0; looq<3; looq++)
    {

        //while (q<3)

        //{
                    try
                    {


                        System.out.print("\n");
                            System.out.print("Please enter the loan amount for loan " + (looq + 1) + " $:");
                            loan = dataIn.readLine();
                            loan1[looq] = Double.parseDouble(loan);
                        }//close try
                        catch(Exception e)
                        {
                            System.out.println("Error in Data" +( "\n") + "Your input is invalid please enter valid number (Ex.200000)");//* added a missing +

                        }//close catch

                    try
                    {
                        System.out.print("Please enter the annual interest rate (Ex:5.0): ");
                        interestRate = dataIn.readLine();

                        interest1 [looq] = Double.parseDouble(interestRate);
                        interest1[looq] = (interest1[looq]/100);
                    }
                    catch(Exception e)
                    {
                        System.out.println("Error in Data" +( "\n") + "Your input is invalid please enter valid number (Ex.200000)");//* added a missing +

                    }//close catch


                    try
                    {
                        System.out.print("Please enter the term of the loan in years: ");
                        period = dataIn.readLine();

                        //term = Double.parseDouble(period);
                        term1 [looq] = Integer.parseInt(period);

                    }//close try

                    catch(Exception e)
                    {
                        System.out.println("Error in Data! Must re-enter loan  " +(looq+1)+ " data" +( "\n")+  "Your input is invalid please enter valid number (Ex.2 or 2.5 or 200)");//****exception error message
                    }//close catch
            q++; //*** increments loop counter

        //}//close while expression

    }//close for loop

//Create Monthly Payment calculation

mPayment[looq]=(loan1[looq]*((interest1[looq]/12)/(1-Math.pow((1+(interest1[looq]/12)),-(term1[looq]*12)))));


//Outputs Table
System.out.print("\n");
System.out.println("Principal\tInterest\tMonths\t\tPayment");

//set counter for print loop
        for (looq=0; looq < 3 ; looq++ )
                {
                //Format output to show readability
                System.out.print("\n");
                System.out.println((looq + 1) + "\t$" + decimalPlaces.format(loan1[looq]) + "\t "
                    + decimalPlaces.format(interest1[looq]*100) + "%" + "\t  " + term1[looq] + "\t$"
                    + decimalPlaces.format(mPayment[looq]));
                System.out.print("\n");
                //looq++;
                }





    }

}

Edited by Dani: Formatting fixed

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by javaAddict
0

At what line do you get that Exception?
And it means that you are trying to access an array with a counter that is bigger than the length.
Check all your arrays and try to debug by putting system.out.println(); before you access an array by printing the counter you are using:
ex:

System.out.println(looq+" "+interest1.length);
interest1[looq];

just to see what values are you passing before the exception

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.