1

I'm getting the following error:

cannot find symbol
                for(Component c : first.caseComponents){
                                       ^
  symbol:   variable caseComponents
  location: variable first of type Component

Here is my code:

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.lang.Iterable;
import java.util.LinkedList;
import java.util.Queue;


public class Composite extends Component implements Iterable<Component>{

    private List<Component> caseComponents = new ArrayList<Component>();

    public Composite(String nameIn, int weightIn) {
        super(nameIn, weightIn);
    }


    @Override
    public Iterator<Component> iterator(){
        return new CompIterator();
    }

    private class CompIterator implements Iterator{
        int index = 0;

        @Override
        public boolean hasNext(){
            if(index<caseComponents.size()){
                return true;
            }
            return false;
        }

        @Override
        public Object next() {
            if(this.hasNext()){
                return caseComponents.get(index++);
            }
            return null;
        }
    }


    public String breadthFirst() {
        Queue<Component> q = new LinkedList<Component>();
        StringBuilder stringRep = new StringBuilder();
        q.add(this);

        while(q.element()!=null){
            Component first = q.remove();
            System.out.println(first.name);
            if (first instanceof Composite){
                Composite comp = (Composite)first;
                for(Component c : comp.caseComponents){

                    q.add(c);
                }
            }


        }
    }

}

And also the Component.java file:

public abstract class Component {

    protected String name;
    protected int weight;

    public Component(String nameIn, int weightIn){
        this.name=nameIn;
        this.weight=weightIn;

    }

    public abstract int getWeight();    //abstract method.

    public abstract String toString();


}

It seems that my type casting does not work and the object comp is still seen as an instance of Component instead of Composite, which has a variable caseComponents. How can I fix this?

2
  • Your error message does not match the posted code, which has no for(Component c : first.caseComponents){ line. Commented May 13, 2015 at 0:45
  • @NathanTuggy: It was there. Now edited to a fixed version. Not sure what the question is about now.... Commented May 13, 2015 at 1:12

1 Answer 1

1

You need to make the loop actually use the typecast:

if (first instanceof Composite){
    Composite comp = (Composite)first;
    // use "comp" here: 
    for(Component c : comp.caseComponents){
             q.add(c);
    }
}

In your code, comp was never actually used anywhere.

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

1 Comment

@Shhua if this is the correct answer, please mark it as such.

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.