Reputation: 267
I have this program in which in the Switch statement structure, I've calculated an expression and want to pass its result to the stack (which I have implemented in Chars). Now the problem is that I have used
char c;
int C = 0;
switch(op)
{
case '+':
{
C = B + A;
c = (char) C;
push(c);
break;
}
case '-':
{
C = B - A;
c = (char) C;
push(c);
break;
}
case '*':
{
C = B * A;
c = (char) C;
push(c);
break;
}
case '/':
{
C = B / A;
c = (char) C;
push(c);
break;
}
}
Now if I print C (which is Integer), it would work just fine, but if I print c (which is char) or I push it to the stack, whatsoever, it would display nothing but empty. I have also used the System.out.println(C) and found out that the calculation ( C = B + A etc) works just fine but the conversion to char (c = (char) C ) does not convert the value.
Any alternative solution would be really helpful. Thanks!
Upvotes: 1
Views: 17293
Reputation: 10959
You are trying to print the numerical value of C
i.e. for the case 2 + 3
and want to print 5
, but when doing the cast of (char) 5
you get the control character ENQ
(See ASCII Characters) which will most likely not be printable by the console.
As given in another answer try
Character.forDigit(C, 10)
But this will only work for single digit answers. If more digits are required you might want to use a string and try
String.valueOf(C)
Upvotes: 4