Reputation:
I'm trying to cast a char that is stored in stack to an integer 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();
Upvotes: 0
Views: 517
Reputation: 62532
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.
Upvotes: 0
Reputation: 533870
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();
Upvotes: 1
Reputation: 10902
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
Upvotes: 1
Reputation: 1503519
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.
Upvotes: 7
Reputation: 61546
Convert the char to an int
Easiest way is this:
operands.push(Integer.parseInt(Character.toString(a)));
Upvotes: 0
Reputation: 31020
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();
Upvotes: 0
Reputation: 89849
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.
Upvotes: 0
Reputation: 10327
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?
Upvotes: 0