1
    public class Packages 
    {
    private ArrayList shipmentList = new ArrayList();
    private double totalWeight;

    public Packages(String shiplist) throws IOException
    {
        Scanner scanFile = new Scanner(new File(shiplist));
        scanFile.useDelimiter("\n");
        while(scanFile.hasNext() == true){
            String pack = scanFile.next();
            Scanner splitter = new Scanner(pack);
            splitter.useDelimiter(" ");
            int id = splitter.nextInt();
            double weight = splitter.nextDouble();
            String state = splitter.next();
            shipmentList.add(new Packet(id, weight, state));
        }
        this.totalWeight = 0;

    }

    public String toString()
    {String allShipments ="";
    for(int i =0;i<shipmentList.size();i++)
        allShipments += shipmentList.get(i) + "\n";
    return allShipments;
    }
    //needs work. Cannot find method isLight() from class packet, yet toString() fires fine.
   public void displayLightPackages()
   {
    for (int i=0;i<shipmentList.size();i++){
       Packet thisShipment = shipmentList.get(i);
        if(thisShipment.isLight() == true){
            System.out.println("Package: "+ shipmentList.get(i).toString()+" Is light.");
        }else{
            System.out.print("");
        }
    }
}

Method displayLightPackages() is giving me errors. here is the packet class:

public class Packet
{
    // instance variables - replace the example below with your own
    private int idNumber;
    private double weight;
    private String state;

    public Packet(int idNumber, double weight, String state)
    {
        this.idNumber= idNumber;
        this.weight = weight;
        this.state = state;
    }
    public boolean isHeavy()
    {
        return (weight>10);
    }  
    public boolean isLight()
    {
      return (weight<10);
    }  
    public String toString()
    {
       return "ID: "+idNumber+" WEIGHT: "+weight+" STATE: "+state;
    }
}

Method toString fires fine on a call from the main method, however, I cannot access any of the other methods from the Packet class. The constructor works when adding packets to the Arraylist containing all packets. I tried defining a packet in the displayLightPackages with

Packet thisShipment = shipmentList.get(i);

but I get an incompatible type error. When just using the code

shipmentList.get(i).isLight()

in the displayLightPackages method, I get an error where the compiler cannot find the symbol isLight. I suspect the error has something to do with the dataType in the arraylist, but other than that, i'm at a loss at this point.

Thank you

2 Answers 2

2

Your ArrayList isn't typed, so everything coming from it is an Object. Conversely, Object doesn't have an isLight() method, hence the "cannot find symbol" error.

To solve this, either type your ArrayList...

private ArrayList<Packet> shipmentList = new ArrayList<>();

... or cast to Packet when you use get:

((Packet)shipmentList.get(i)).isLight()

Of the two options, typing the ArrayList is simpler and safer, since you don't have to remember to cast every time you're getting a value from it.

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

1 Comment

wow, thanks man. Completely solved my problem and I learned something! thanks again.
2

Change

private ArrayList shipmentList = new ArrayList();

to

private ArrayList<Packet> shipmentList = new ArrayList<Packet>();

Compiler does not know that shipmentList holds Packets. In fact, compiler know that shipmentList is a list of an Objects. So you are able to do shipmentList.get(i).toString(), because toString() is method from Object class. But you can't invoke isLight() method on object because it doesn't exists in Object context. You must explictly say that shipmentList is type of ArrayList<Packet>, or cast element to Packet: Packet thisShipment = (Packet) shipmentList.get(i);

Comments

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.