Upon compiling, I get the error of cannot find symbol - method add(java.lang.String). The goal of the code so far is just to add a new flavour to the ArrayList
. I am using the BlueJ IDE, monstrous I know!
import java.util.ArrayList;
public class IceCream
{
// stores ice cream flavours.
private ArrayList<String> flavours;
/**
* Constructor for objects of class IceCream
*/
public IceCream()
{
// initialise instance variables
flavours = new ArrayList<String>();
}
/**
* Method to add a flavour to the list.
*/
public void newFlavour(String flavours)
{
flavours.add(flavours);
}
}
You named the String parameter equal to the attribute's name (flavours), so the compiler tries to lookup the add() method in the String class (which is not available). Change the code like:
public void newFlavour(String flavours)
{
this.flavours.add(flavours);
}
or use different names.