1

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!

4
  • Character.toChars(integer?) Commented Jan 28, 2014 at 18:57
  • For what values of A and B? or more so what is the value of C that you are trying to print? are you trying to print the number of the character value of that number? Commented Jan 28, 2014 at 19:01
  • The values of A and B are 2 and 3 respectively. The result should be according to the op which is either +, -, * or /. The variable C (integer) stores the result accurately but I am not able to convert this integer value (C) into char value(c) Commented Jan 28, 2014 at 19:03
  • Go the other way when printing, so that you're not trying to print out ASCII 2 or 3 (which aren't printable). System.out.println("" + ((int) c)); or anything like that, that converts your char to an int first. Commented Jan 28, 2014 at 19:08

2 Answers 2

4

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)
Sign up to request clarification or add additional context in comments.

2 Comments

you are right, I didn't post the String.valueOf(C) because he said that he has a stack of char, so I think that he should know that the result must be a single-digit integer
@Alessio Yeah I noticed that too. Question looks like part of some form of calculator so I'm hope the OP is aware of any multi-digit possibilities. which could come about very easily, especially with *
3
Character.forDigit(C, 10)

Try this, it should work.

1 Comment

I did that and it said that the the types are incompatible i.e. Required: char, whereas Found: Char[]

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.