0

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.

2
  • Override toString in your FillUp class. Commented Nov 5, 2014 at 17:49
  • Yes, you're printing out the first element of the list, which is a FillUp. But you haven't overridden toString() in the FillUp class, which is why you're getting output like that... For your loop, obtain an ArrayList once instead of calling fillUpArray in your loop. Commented Nov 5, 2014 at 17:50

1 Answer 1

1

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++;
        }
Sign up to request clarification or add additional context in comments.

3 Comments

gave this a try, getting this error.... Once again, I feel like List is not filling or populating correctly.... Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:635) at java.util.ArrayList.get(ArrayList.java:411) at lab08.MileageCalculator.main(MileageCalculator.java:17) Also, with the SOP it prints out the same as before, lab08.FillUp@1647ad40 Which I feel like I need to add a toString....
@Loomiss There was another call to fillUpArray inside the loop which I forgot to replace. If you didn't replace it either, it might explain the error. See my edit.
Thank you very much, it works now. I know what is happening (like all the commands and such) I just couldn't figure out the specifics behind the error which was frustrating to me. I am also on a limited time line (Recert'ing my medical army license all week...) can you point me to a good/in-depth article/source of for reading up on ArrayList/Arrays in general to make sure I have a good base knowledge on this stuff?

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.