Results 1 to 14 of 14
- 11-01-2008, 12:12 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
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;
}
}
-
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.
- 11-01-2008, 01:56 AM #3
The error message says it all:incompatible types
found : java.lang.Object
required: char
topOp = oStack.pop();
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!
- 11-01-2008, 02:22 AM #4
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
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...
- 11-01-2008, 01:20 PM #5
Try debugging your code to see what is returned by the pop by doing a println() on what's returned.
Show the code that does the push.I have been pushing char's into the stack
The compiler does some conversion(boxing) without telling you and that can be a confusion.
- 11-01-2008, 05:03 PM #6
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
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
- 11-01-2008, 05:50 PM #7
How did you do that? I don't see any System.out.println() statements in your code!I tried to see what it was popping
Object obj = stack.pop();
System.out.println("obj=" + obj); // see what we get
What is the type of oStack?
-
much progress has been made in the Sun Java forums thread.
- 11-01-2008, 07:31 PM #9
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.
-
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.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.
- 11-01-2008, 11:33 PM #11
My issue is that boxing in this case is not symmetrical.a generic Stack<Character> then this issue would be solved.
For example this compiles:Here a char is pushed on the stack. No reference to Character.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
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.
-
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".
-
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.
My question though, is where is the asymmetry and how is it manifesting itself here? Thanks!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()); } }
Pete
- 11-02-2008, 02:00 PM #14
Similar Threads
-
How can i insert a char into a string
By Jamie in forum New To JavaReplies: 8Last Post: 02-17-2011, 08:59 PM -
char to string
By kian_hong2000 in forum New To JavaReplies: 2Last Post: 08-25-2008, 01:51 PM -
Assigning a string value to a char
By coffeebean in forum New To JavaReplies: 4Last Post: 06-15-2008, 06:30 AM -
Casting an int value into a char
By kurtulas in forum New To JavaReplies: 2Last Post: 02-16-2008, 08:03 PM -
Help with, String, Char
By lenny in forum New To JavaReplies: 1Last Post: 07-25-2007, 02:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks