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();
}
trips?counter < tripswill fix