Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stack.peek() Error

 
nani aman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all friendly ranch hand. I have an error which i couldn't figure out. It look simple but i still couldn't solve it by myself.

error 1
symbol : method peek ()
location: class Stack
top = stack.peek();

error 2
symbol : method equal (java.lang.String)
location: class java.lang.String
else if(!(top.equal(c1))) {stack.push(c1);}

this is my part of code:



i have import java.lang.String and declare the stack.
So why this error occurs?

Thanks kind ranch hands.

[ changed \ to / -ds ]
[ October 23, 2004: Message edited by: Dirk Schreckmann ]
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both messages mean that the compiler cannot find a method with a matching name and signature (sequence and types of parameters).Since that says "Stack" and not "java.util.Stack", I assume Stack is your own class. Check the method name and signature. That looks like a correct definition of a peek() method, but without Stack's definition I can only guess.This one is looking for a method named "equal" in class String that takes a String (or one of its superclass/interfaces, like Object). Look in the JavaDocs for String and you'll see that it overrides the method "boolean equals(Object)" from Object (note the "s").

Here's a free pointer:This is equivalent to
[ October 22, 2004: Message edited by: David Harkness ]
 
nani aman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i use Class Stack in java.util and i already using java.util.*. For stack.peek(), it receive String data type and i put at variable top to compare. I have see the JavaDoc but how to define wrong?

wish you can guide me. :roll:
 
Jimmy Die
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


your method definition equal() correct?

Or you should equals() with an 's'.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nani aman:
Actually i use Class Stack in java.util and i already using java.util.*. For stack.peek(), it receive String data type and i put at variable top to compare. I have see the JavaDoc but how to define wrong?
Given that the error message says "class Stack" and not "class java.util.Stack" (note that the other error message says "java.lang.String"), I would guess you have a variable named "Stack" with a capital S or you have defined your own Stack class.

If you post your code we'll be able to help you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic