5

My code works perfectly as far as I can see but I still get the error "Cannot cast from Object to char" and I was wondering if anyone could explain to me what the problem is and if I can do it another way that does not cause an error.

This is the bit of code causing the error char op = (char) term;. It is from an infix to postfix converter

//Take terms off the queue in proper order and evaluate
private double evaluatePostfix(DSAQueue<Object> postfixQueue) {
    DSAStack<Double> operands = new DSAStack<Double>();
    Object term; 

    //While still terms on queue, take term off.
    while(!postfixQueue.isEmpty()) {
        term = postfixQueue.dequeue();
        if(term instanceof Double) { //If term is double put on stack
            operands.push((Double) term);
        }
        else if(term instanceof Character) { //If term is character, solve
            double op1 = operands.pop();
            double op2 = operands.pop();
            char op = (char) term;
            operands.push(executeOperation(op, op2, op1));
        }
    }
    return operands.pop(); //Return evaluated postfix
}

Any help (even pointing me to some reading) would be much appreciated.

Maroun
  • 94,125
  • 30
  • 188
  • 241
user2963286
  • 89
  • 2
  • 2
  • 8
  • Tried casting to `Character` instead? – Evan Knowles Apr 30 '14 at 08:12
  • 1
    [Java Tutorial: Language Basics](http://docs.oracle.com/javase/tutorial/java/TOC.html), at the very least read about primitive data types and objects. – Oleg Estekhin Apr 30 '14 at 08:12
  • No, no I had not. I feel incredibly stupid now haha but thank you @EvanKnowles – user2963286 Apr 30 '14 at 08:15
  • You probably run it on JDK 6, since JDK7 this is allowed. You can read more about this in http://stackoverflow.com/questions/16119638/differences-in-auto-unboxing-between-java-6-vs-java-7 – endriu_l Apr 30 '14 at 08:24

5 Answers5

8

You can change this line:

char op = (Character) term;

Explanation: in Java 6 you can't cast an Object to a primitive type, but you can cast it to Character (which is a class) and unboxing does the rest :)

Edit: Alternatively, you can bump the language level of your project up to Java 7 (or 8...)

vikingsteve
  • 38,481
  • 23
  • 112
  • 156
  • 1
    As I know, you can cast objects to primitive datatypes in jdk1.6 – Parasu Apr 30 '14 at 08:15
  • 1
    Actually it got changed in JDK1.7 JLS http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.5, Java 7 allows unboxing of Object to primitive – endriu_l Apr 30 '14 at 08:22
1

There is nothing wrong with your code. Since term is an instance of Character you could even assign it directly without casting as char op = term if you are using jdk 1.5+

One possibility i could see is the compiler compliance level used. You might have a JDK 1.5+ but you are compiling your project in a lower level. If you are using eclipse check for JDK compliance level in Project->properties->Java Compiler

If you are directly compiling or using Ant build check for -source & -target options.

And just for your info, as far as i know Object to primitive casting was introduced in JDK 1.7. So if you are using JDk 1.7+ you could wirte as

Object term = 'C';
char op = (char) term;
Syam S
  • 8,421
  • 1
  • 26
  • 36
0

Java can't auto unbox and cast in one operation, which is what you're trying to do. Instead, cast it to Character and java will auto unbox it OK:

char op = (Character) term;
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

You can't cast from an object to a primitive data type (or vice versa). They are two very different things.

Java lang package has classes that correspond to each primitive: Float for float, Boolean for boolean..

As others already stated, you should cast the object to Character and not char.

Maroun
  • 94,125
  • 30
  • 188
  • 241
0

you have to chang

char op = (Character) term;

because you have to cast with class refference not using the primitive type.

MadukaJ
  • 722
  • 6
  • 22