1

I'm very new to Java, currently learning arrays. So I was making this little program to input gas used and miles traveled to calculate miles per gallon, but whenever I run the program I get an error at line 21 (miles[counter] = input.nextInt();) the error says:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at GasMileage.inputGasAndMiles(GasMileage.java:21)
at GasMileage.main(GasMileage.java:44)

I have no idea what this means nor do I know how to fix it, It'd be great if I could get some help on this.

int counter = 0;
int[] gallons, miles = new int[trips];

public void inputGasAndMiles(){

    for(counter = 0; counter <= trips; counter++){
        System.out.print("\nInput miles traveled: ");
        miles[counter] = input.nextInt();

        System.out.print("Input gallons of fuel used: ");
        gallons[counter] = input.nextInt();
    }
}

EDIT

public void askTrips(){
    System.out.print("How many trips would you like to calculate for: ");
    trips = input.nextInt();
}

Stack trace:

public static void main(String[]args){
    GasMileage gas = new GasMileage();

    gas.askTrips();
    gas.inputGasAndMiles();
    gas.calculate();
    gas.display();
}
4
  • What is the initial value of trips? Commented Mar 27, 2013 at 17:57
  • counter < trips will fix Commented Mar 27, 2013 at 17:57
  • also, don't forget to initialize gallons. Commented Mar 27, 2013 at 18:00
  • @Legend the initial value is set by the user, I forgot to show the code above. Commented Mar 27, 2013 at 18:01

5 Answers 5

3

it should be for (counter = 0; counter < trips; counter++)

because, array index starts from zero, so max index would be (size-1) not size

EDIT:

int trips= 0; //any +ve value
int[] gallons =  new int[trips], miles = new int[trips];

public void inputGasAndMiles(){

for(counter = 0; counter < trips; counter++){
    System.out.print("\nInput miles traveled: ");
    miles[counter] = input.nextInt();

    System.out.print("Input gallons of fuel used: ");
    gallons[counter] = input.nextInt();
}

}

Sign up to request clarification or add additional context in comments.

8 Comments

This does not provide the same error. trips is most likely 0, resulting in the index out of range at index 0.
still it wont go out of index
It does not answer his question. An array of size 0 does not have an element at index 0.
@WotWat, make sure trips is designated initially above 0.
@Legend then how will it pass the case 0<0 ? (where 1st 0 is start index, and 2nd is size)
|
0

Change this

counter <= trips

to this

counter < trips

in the for construct there.

Comments

0

Change

for(counter = 0; counter <= trips; counter++)

to

for(counter = 0; counter < trips; counter++)

index of array start from 0 to (length -1 )

Comments

0

Try this

int counter = 0;
int[] gallons, miles = new int[trips];

public void inputGasAndMiles() {
    for(counter = 0; counter < trips; counter++) {
        System.out.print("\nInput miles traveled: ");
        miles[counter] = input.nextInt();

        System.out.print("Input gallons of fuel used: ");
        gallons[counter] = input.nextInt();
    }   
}

Comments

0
for(counter = 0; counter <= trips; counter++)

change it to :

for(counter = 0; counter < trips; counter++)

The size of your gallons array is trips .Since the first Index starts with 0 so the last index of your gallons array will be trips-1. And in your code you are trying to access the element at index trips when (counter == trips) in for loop becomes true, this leading to ArrayIndexOutOfBounException

1 Comment

Yes, remember, arrays start at 0 not 1, so if you want to iterate over an array of 10 elements, you need to go from 0 to 9

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.