Reputation: 11
We are creating an ArrayList
from a File, which contains both integers (odometer reading) and doubles (gallons per fill up). We're using them to make a mileage. We are then using a container class to access them, so on and so fourth.
The container looks like this:
package lab08;
public class FillUp{
private final int odometer;
private final double gallons;
public FillUp(int givenOdometer, double givenGallons){
odometer = givenOdometer;
gallons = givenGallons;
}
public int getOdometer(){
return odometer;
}
public double getGallons(){
return gallons;
}
}
My code looks like the following:
public class MileageCalculator {
public static void main(String[] args) throws FileNotFoundException{
Scanner scan = new Scanner(new File("/Users/Mobile_Hive/Desktop/Homework/mileage.txt"));
double mileage=0.0;
int i=0;
System.out.println(fillUpArray(scan).get(0));
/**
while(i+1<fillUpArray(scan).size()){
mileage=(((FillUp)fillUpArray(scan).get(i+1)).getOdometer()-((FillUp) fillUpArray(scan).get(i)).getOdometer())/((FillUp) fillUpArray(scan).get(i+1)).getGallons();
System.out.println("Mileage: "+mileage);
i++;
}
**/
}
private static ArrayList<FillUp> fillUpArray(Scanner scan){
ArrayList<FillUp> list = new ArrayList<FillUp>();
int odometer=0;
double gallons=0.0;
while(scan.hasNextLine()){
String line = scan.nextLine();
Scanner scans = new Scanner(line);
while(scans.hasNext()){
odometer=scans.nextInt();
gallons=scans.nextDouble();
}
FillUp fill = new FillUp(odometer, gallons);
list.add(fill);
}
return list;
}
I am using the comments to occlude off my method for getting the actual mileage calculation and attempting to see if I can even access indexes of the ArrayList I have attempted to make.... When I run this in its current form I get:
lab08.FillUp@3bbf502d
When I remove the comments around "while(i+1< ) etc" leading to "i++" I get:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at lab08.MileageCalculator.main(MileageCalculator.java:15)
I feel like in the first situation, I am not accessing the index of the array list, almost like it has not been filled. In the second attempt, I am not quite sure what is happening. I am not looking for straight forward answers, but maybe just explanation as to what is happening with very stripped down instructions on what needs to happen. I also would love information on what is happening so if someone can push me to some reading material that would be awesome.
Upvotes: 0
Views: 288
Reputation: 393821
Each time you call fillUpArray(scan)
you are getting a different ArrayList
, whose size may be completely different than what you got in the previous call to that method. You should call that method once and store its output in a variable, so that you use the same list.
List list = fillUpArray(scan);
System.out.println(list.get(0));
while(i+1<list.size()){
mileage=(((FillUp)list.get(i+1)).getOdometer()-((FillUp) list.get(i)).getOdometer())/((FillUp) list.get(i+1)).getGallons();
System.out.println("Mileage: "+mileage);
i++;
}
Upvotes: 1