tags:

views:

312

answers:

8

I'm trying to cast a char that is stored in stack to an interger and this is what I did.

operands = new StackLinked();

if ( (a == '0') || (a == '1') || (a == '2') || (a == '3') || (a == '4') || 
     (a == '5') || (a == '6') || (a == '7') || (a == '8') || (a == '9') )
{
   operands.push(a);   /*Stor operands in the stack operands.*/
}

//This line crushes my program. I don't know why.
int op1 = ((Integer)operands.peek()).intValue();
+6  A: 

You haven't shown the declaration for a but I suspect it's of type char. That's then autoboxed to Character, and so when you cast to Integer the cast fails.

If you change your code to use:

operands.push((int) a);

that should convert from char to int then box to Integer and you'll be away.

Alternatively, use:

// Implicit conversion from char to int
int op1 = ((Character) operands.peek()).charValue();

EDIT: Note that the above solutions would both end up giving op1=49 when a='1', op2=50 when a='2' etc. If you actually want op1=1 when a='1' you could either use Character.digit or (as we already know that 'a' is in the range '0' to '9') you could just subtract '0', i.e.

operands.push((int) (a-'0'));

or

int op1 = ((Character) operands.peek()).charValue() - '0';

In the first case the cast is actually then redundant, as the result of the subtraction will be int rather than char - but I'd leave it there for clarity.

Jon Skeet
The implicit conversion gives you a 1 for the character '1' or a 49 (ASCII code for '1')?
Martinho Fernandes
Yes, it does. This may or may not be the desired operation. We're not working with much information here :(
Jon Skeet
To clarify, the conversion of '1' results in the int value of 49.
Mark Renouf
Yup. I'll edit the answer to explain this.
Jon Skeet
I found a "warning" in one of Jon Skeet's answers! I rock!
Martinho Fernandes
A: 

Are you sure you're actually pushing anything onto the stack? If not then your casting line with throw EmptyStackException. What exception are you getting?

colithium
A: 

Your example may be a little ambiguous, here are my assumptions:

  • You are using Java 1.5 langauge features (due to boxing/unboxing).

  • operands is behaving similar to Stack<Character>();

When you push a char into the stack, it's boxed as a Character. You can't cast Character instance to an Integer. You must use get the primitive numeric value, then cast it to an integer primitive.

Try this:

int op1 = (int) operands.peek().charValue();
Mark Renouf
A: 

This is really hard without seeing the definition of StackLinked, is that one of your own classes?

I can think of several options: The stack is somehow empty (e.g., a was not one of these characters').

Or the 'a' is saved as a Character in the stack (rather than as a char), and then the conversion to Integer fails. You may need to first get the char out of the Character.

Uri
A: 

You're pushing a Char, not an Integer. You need to say something like this:

Char op1 = (Char)operands.peek(); int opcode = Char.digit(op1.charValue(),10);

(my Java is a bit rusty, so sorry if some of the method names are slightly wrong)

Also, is your Peek method returns null (becuase your if statement evaluated to false) then you code will crash.

Sean
A: 

Convert the char to an int

Easiest way is this:

operands.push(Integer.parseInt(Character.toString(a)));
TofuBeer
+1  A: 

You must be using 1.5. So here's one more way to get it right:

if (a >= '0' && a <= '9') {
    operands.push(a);
}

char c = operands.peek();
int op1 = (int) c; //check Character.isDigit(c), if there is other stuff in the stack
+1  A: 

The simple way to convert a char to an int value is to do

if (a >= '0' && a <= '9') {
    operands.push(a - '0'); // converts a char to an int.
}
int op1 = (Integer) operands.peek();
Peter Lawrey