Re: Help with comparing objects
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Taria schreef:
On Apr 3, 1:07 pm, "Daniel Pitts" <googlegrou...@coloraura.com> wrote:
On Apr 3, 4:00 pm, "Taria" <mche...@hotmail.com> wrote:> I have been working on the proverbial postfix program for awhile and
found this error. I am having trouble comparing objects with a
value. I tried to put the value into an object structure but it still
doesn't work. It pops beyond where I expect then has an out of
bounds error.
This is a simpler version of my program, please don't laugh. I have
cut and pasted out the problem area. The "while" condition never
returns true when it should. hrmph. Also, our professor required we
write our own Stack class instead of using java's stack class..so I
included that as well. Any hints...is appreciated.
while (operatorStack.top() != LParenthesis){
Hint #1:
don't use !=, use !operatorStack.top().equals(LParenthesis)
Hint #2:
You are pushing Strings, and comparing to Expressions...
Try using:
while (!operatorStack.top().equals("("))
You know I tried the above on my mini program and it works like a
charm. I inserted the tweaked corrected code into my main program and
it still skips it :(. Thank you very much for your help. If I can
get it to work in my mini program, I should soon be able to make the
bigger one cooperate too.
The main program pushes characters into a stack as follows:
for (int i = 0;i < size; i++){
char ch = infixInput.charAt(i);
if (ch == '+' || ch == '-' || ch == '/' || ch == '*' || ch
== '('){
operatorStack.push(ch);
}
...
}
So, my mini program is different in that way, does it make that much
of a different whether it's a character or string you're pushing onto
it? I'm getting confused! I'll play around with it a bit more
Yes, it makes a difference. Strings are objects, so they have reference
semantics. The confusing thing here is that the JVM tries everything to
not let that be a problem, by caching strings. However, it is still
always a good idea to do myString.equals(someString), instead of using
!=, since ???you never know???[*]. chars on the other hand are not objects,
that is you *have to* compare them using !=. But since you push chars
in a stack, autoboxing pops in too. That means the Stack stores
Character instances, no chars. Those are objects again, you can compare
them with equals, and probably should.
Another problem is that you use some self-defined Expression class to
compare with. Then it all depends on how the involved classes define
equals(). Since you invoke equals on the String, it will most
definitely return false, since String is implemented that way.
My hints: look up autoboxing, reference vs. value semantics and think
about why you need the ???Expression??? class, and whether you wouldn???t use
a Stack<Expression>.
HTH, H.
[*] i.e. there are cases where two strings with the same content are not
the same. It is well-defined when this happens, but confusing to
beginners, so I???ll leave it out here.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
iD8DBQFGE11Xe+7xMGD3itQRAoG+AJ4nkQIfG9fJ5+OkFTugpbFpoLWemwCfaV0s
9qLBQnRlzuL1SLwB1nPyycU=
=UY+O
-----END PGP SIGNATURE-----