0

I am having trouble seeing the error of my work: I am creating an iterator (in my class LamborghiniCarLot) which will give the number of vehicles in my (ArrayList inventory;) which are rear-wheel-drive. the Lamborghini class has a method called getIsRearWheelDrive(). But attempting to call it, I get the method not found. "inventory" is the field variable holding the arraylist. Here is my code as it stands:

public int howManyAreRearWheelDrive()
{
    int numberRearWheel = 0;
    Iterator<Lamborghini> it = inventory.iterator();
    Lamborghini inv = null;

    while(it.hasNext())
    {
        inv = it.next();
        if(inv != null)
        {
            if(it.getIsRearWheelDrive() == true)
            {
                numberRearWheel ++;
            }
        }
    }
    return numberRearWheel;
}
0

3 Answers 3

2
if(it.getIsRearWheelDrive() == true)

should be

if(inv.getIsRearWheelDrive() == true)
Sign up to request clarification or add additional context in comments.

1 Comment

or just if(inv.getIsRearWheelDrive()).
0

Instead of it.getIsRearWheelDrive() you need to use inv.getIsRearWheelDrive() since your Lamborghini class has the method. it is your iterator and an iterator does not have a getIsRearWhileDrive() method.

Comments

0

I would use a for loop

for(Lamborghini inv: inventory) {
  if(inv.getIsRearWheelDrive()) numberRearWheel++;
}

or a Stream

public int howManyAreRearWheelDrive() {
  return (int)inventory.stream().filter(inv -> inv.getIsRearWheelDrive()).count();
}

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.