0
public LightsOutBFS(){
    //readtext
    int[] arr = new int[25];
    int[] state = new int[25];
    int[] soln = new int[25];

    boolean check=true;
    PriorityQueue<Node> q = new PriorityQueue<Node>();

    //Reading the text file
    try{
        FileInputStream fstream = new FileInputStream("switches.txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        int i = 0;

        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   {
                //tokenize strline
                StringTokenizer st = new StringTokenizer(strLine, " \n"); 
                while(st.hasMoreTokens()) { 
                arr[i]=Integer.parseInt(st.nextToken());
                i++;
            }
        //Close the input stream
        }in.close();


    }catch (Exception e){//Catch exception if any
         System.err.println("Error: " + e.getMessage());
    }
    for(int i=0; i<25; i++){
        state[i]=0;
        soln[i]=0;
    }
    //loop that flips the adjacent side of switches turned on
    for(int i=0;i<25;i++){
        if(arr[i]==1)
            method.flip_adjacent(i,state);
    }


    //implement bfs here
    parent = new Node(state,0,soln,null);


    q.offer(parent);
    while(check){
        while(!q.isEmpty()){
            Node x = q.poll();
            int depth = x.depth;
            int posMoves = 25-depth;
            for(int i=0; i<posMoves;i++){
                current = generateNode(x.state,depth,x.soln,x);
                if(EtoNaYun(current.state))
                    check=false;//check state;
                q.offer(current);
            }
        }
    }

}

I am trying to use Class Priority Queue and typecast it as Node Object but my code shows this exception: java.lang.ClassCastException: Node cannot be cast to java.lang.comparable. Any idea? Is it wrong to typecast priority queue as an object? thanks in advance!

2
  • Which line gives the error? Commented Dec 8, 2012 at 5:59
  • it doesn't says explicitly which line has the error it just prints the ClassCastException Commented Dec 8, 2012 at 6:04

1 Answer 1

1

It's very clear from the error message that your program is failing because the Node class you are using doesn't implement Comparable<T> interface. Without Comparable<T> interface PriorityQueue will have no idea how to orders elements (Node objects).

Solution:

Make your Node class to implement Comparable interface and override public int compareTo(Obj o); to compare the Node based on some id/priority (I don't know the definition of your Node class, but may be x.depth?)

public Node implements Comparable<Node> {
   ...
   @Override
   public int compareTo(Node o) {
    return this.priority > o.priority;
   }
}
Sign up to request clarification or add additional context in comments.

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.