Reputation: 89
I get the following errors: the method getOdometer & getGallons are undefined for the type Object. Here is my code. My issue is in the main method, in the while loop where I'm using my accessor methods. Why isn't this working and how can I fix it? I'm new to programming so don't give me any answers that are too complicated. Thank you for your answers.
The program I'm trying to make reads from a file and grabs the odometer and gallons that the car filled up at that odometer. I was told to use a helper method to grab those numbers from the file and calculate the mileage in the main method after I return the arraylist of FillUp objects from my FillUpArray helper method.
package lab8;
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class CheckpointOne {
public static void main(String[] args) throws FileNotFoundException
{
File file = new File("C:/Users/Nick/workspace/project8/mileage.txt");
Scanner scan = new Scanner(file);
double mileage=0.0;
int i=0;
while(i+1<fillUpArray(scan).size())
{
mileage=(fillUpArray(scan).get(i+1).getOdometer()-fillUpArray(scan).get(i).getOdometer())/fillUpArray(scan).get(i+1).getGallons();
System.out.println("Mileage: "+mileage);
i++;
}
}
private static ArrayList 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;
}
}
And this is my FillUp class that I'm using.
package lab8;
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;
}
}
Upvotes: 0
Views: 8835
Reputation: 354
The problem is that you have use raw type in the value returned by the static method
private static ArrayList fillUpArray(Scanner scan)
. The raw types allow to specify a collection without define the generic type over the collection is built. You can take a look to the raw type here. It allows maintaint the compatibility with the oldest JVM (version < 5.0).
So, if you use raw type you force tot he collection to use items which are casting to Object. In this case you define that fillUpArray(Scanner scan)
returns ArrayList
element, and I have already mentioned that it is a raw type. Thus, you manage the return value like a ArrayList.
Of course, the dynamic binding, define that in runtime the message will be sent to the target object, FillUp
in this case. However, the type of this element are solve in static-time. So, the compiler checks the static type of the elements of the Array returned by the fillUpArray
, and you have used raw types so the dynamic type will be solver to Object.class
. And the compiler sees that Object elements do not contains any method called fillUpArray
.
The solution, you can change the signature of the method form:
private static ArrayList fillUpArray(Scanner scan).
to
private static ArrayList< FillUp> fillUpArray(Scanner scan).
You can see a similar example of the dynamic and static types.
Upvotes: 1
Reputation: 4476
mileage=(fillUpArray(scan).get(i+1).getOdometer()-fillUpArray(scan).get(i).getOdometer())/fillUpArray(scan).get(i+1).getGallons();
The above line tries to call getOdometer() and getGallons()
using an instance Object
class.
You have to downcast to FillUp
object before calling that method.
The modified code will be:
mileage=((FillUp)fillUpArray(scan).get(i+1).getOdometer()-(FillUp)fillUpArray(scan).get(i).getOdometer())/(FillUp)fillUpArray(scan).get(i+1).getGallons();
Note : better practice would be to return the same list of Objects
as desired.
Upvotes: 1
Reputation: 178263
In the fillUpArray
method, you created and returned an ArrayList<FillUp>
, but the return type on the method is the raw type ArrayList
. Because it's raw, type erasure indicates that the return type of get
on the raw ArrayList
will be Object
, which certainly doesn't have those 2 methods getOdometer()
and getGallons()
.
Change
private static ArrayList fillUpArray(Scanner scan)
to
private static ArrayList<FillUp> fillUpArray(Scanner scan)
Upvotes: 2
Reputation: 500357
You need to return ArrayList<FillUp>
from fillUpArray()
:
private static ArrayList<FillUp> fillUpArray(Scanner scan)
^^^^^^^^ THIS
Without this, you are effectively returning an array list of Object
.
Upvotes: 1