Results 1 to 14 of 14
  1. #1
    viligante8 is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default pop from stack and store to char?

    I am trying to write an infix to postfix prgm and when i compile i get:

    ListOperations3.java:143: incompatible types
    found : java.lang.Object
    required: char
    topOp = oStack.pop();

    because apparently the built in stacks use generic objects as a return type and thats incompatible with char. How can i pop from the stack to a char?

    Here is a method where the problem arises:

    public void Parenthesis(){
    char ch;
    char topOp;
    while(!oStack.isEmpty()){
    topOp = oStack.pop();
    if(ch == '(')
    break;
    else
    postFix = postFix + ch;
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    To the original poster, cross-posting can frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be to not do this, but if you feel that you absolutely must, to at least provide links in both cross-posts to each other.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    incompatible types
    found : java.lang.Object
    required: char
    topOp = oStack.pop();
    The error message says it all:
    pop returns an Object.
    topOp is char. A char is a primitive type holding a single character
    incompatible!!!

    How did you get a char type on the stack? Stacks only hold objects!

  4. #4
    viligante8 is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default Sorry

    Sorry about the cross posting, i just needed a response quick.

    I have been pushing char's into the stack so I assume that the stack contains char values. If stacks only hold objects then how do I work with the objects in the stack and the char's from the infix expression?

    The only thing i could think of was maybe it has something to do with wrapper classes but i dont know how those work...

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    Try debugging your code to see what is returned by the pop by doing a println() on what's returned.
    I have been pushing char's into the stack
    Show the code that does the push.
    The compiler does some conversion(boxing) without telling you and that can be a confusion.

  6. #6
    viligante8 is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default pop from stack to char

    public void Operator(char thisOp, int thisPrec){
    int topPrec;
    char topOp;
    while (!oStack.isEmpty()){
    topOp = oStack.pop();
    if(topOp == '('){
    oStack.push(topOp);
    break;
    }
    else {
    if(topOp == '+' || topOp == '-')
    topPrec = 1;
    else if(topOp == '*' || topOp == '/')
    topPrec = 2;
    else if(topOp == '^')
    topPrec = 3;
    if(topPrec < thisPrec){
    oStack.push(topOp);
    break;
    }
    else
    postFix = postFix + topOp;
    }
    }
    oStack.push(thisOp);
    }


    I tried to see what it was popping but it got too confusing for me...thanks in advance for everything

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    I tried to see what it was popping
    How did you do that? I don't see any System.out.println() statements in your code!

    Object obj = stack.pop();
    System.out.println("obj=" + obj); // see what we get

    What is the type of oStack?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    much progress has been made in the Sun Java forums thread.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    This thread has brought up an issue for me. When the compiler does something FOR YOU (or is it TO YOU) like boxing, it should put out an info message saying what it has done. It seems that boxing in this case is not symmetrical. The char is converted to a Character in the push() but not back to a char in the pop. This leaves the student wondering what has happened.
    To save a few key strokes, the compiler writers have done this trick and its OK for student code. BUT I don't agree with it for production code. I thought one of the reasons java didn't have macros was because of this type of problem: hiding generated code.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It seems that boxing in this case is not symmetrical. The char is converted to a Character in the push() but not back to a char in the pop.
    But how is the compiler to know that what pops out is a Character? I think the problem is that the code doesn't specify this. If the code used a generic Stack<Character> then this issue would be solved.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    a generic Stack<Character> then this issue would be solved.
    My issue is that boxing in this case is not symmetrical.
    For example this compiles:
    Java Code:
    	  Stack<Character> stk = new Stack<Character>();
    	  char aChar = 'a';                  // use a primitive not an Object
              stk.push(aChar);                  // object required here, not primitive - boxed by 1.6
    Here a char is pushed on the stack. No reference to Character.
    Now how do I get the char back? I have to KNOW that the compiler has done this TO ME. It doesn't tell me that it has generated some code. If it knows going in, then why not coming out???
    To me its a 'religious' question. It should be symmetrical!
    I studied compiler writing getting my MS and worked on a project that wrote a compiler for a micro procressor.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'll have to plead ignorance here as I'm not nearly as knowledgeable as you are on this subject to be able to debate principle, but I guess that when it comes to code-religion here, I'm more of "pragmatic agnostic".

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'm curious as to just what you mean by it being asymmetric. For instance with this code that uses auto-boxing and auto-unboxing, it appears that every box is matched by an unbox, but occurring without the programmer being notified or warned or whatever.
    Java Code:
    import java.util.Stack;
    
    public class BoxSymmetry
    {
      public static void main(String[] args)
      {
        String string = "Supercalifragilisticexpialidocious is longer " +
        		"than Antidisestablishmentarianism";
        Stack<Character> charStack = new Stack<Character>();
        for (int i = 0; i < string.length(); i++)
        {
          // here's where the auto-boxing occurs
          charStack.push(string.charAt(i));
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < string.length(); i++)
        {
          // here's where the auto-unboxing occurs.
          // it appears to be "symmetrical" to me.
          char c = charStack.pop();
          sb.append(c);
        }
        System.out.println(sb.toString());
        
      }
    }
    My question though, is where is the asymmetry and how is it manifesting itself here? Thanks!
    Pete

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    Your right!!!. It does work (as it should). I'd left the generics bit off the Stack<Character> definition in one of my tests and forgot to put to back as I made other changes.
    Thanks.

Similar Threads

  1. How can i insert a char into a string
    By Jamie in forum New To Java
    Replies: 8
    Last Post: 02-17-2011, 08:59 PM
  2. char to string
    By kian_hong2000 in forum New To Java
    Replies: 2
    Last Post: 08-25-2008, 01:51 PM
  3. Assigning a string value to a char
    By coffeebean in forum New To Java
    Replies: 4
    Last Post: 06-15-2008, 06:30 AM
  4. Casting an int value into a char
    By kurtulas in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 08:03 PM
  5. Help with, String, Char
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 02:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •