Results 1 to 18 of 18
  1. #1
    Tom_Snake is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Java Stack problems, return of peek().

    Hey guys, i'm in a daze right now. I have a class called Room, and would like to store these in a stack. I've implementet the pop & push correctly (at least I think so), but the peek won't return what I expected.

    Java Code:
         /**
         * peek at the top of the stack
         */
        public void peekStack(){
            if(!stackEmpty()){
                currentRoom = previousRoom.peek();
            }
        }
    I also tried:

    Java Code:
        /**
         * peek at the top of the stack
         */
        public Room peekStack(){
            if(!stackEmpty()){
                return previousRoom.peek();
            }
        }
    previousRoom is the name of the stack, just to clarify :)

    The console tells me that they are incompatible types, but shouldn't peek return the same object as I pushed into the stack?

    The push method is as follows:
    Java Code:
      
        /**
         * push the stack
         */
        public void pushStack(Room room){
            previousRoom.push(room);
        }
    What on earth am I doing wrong?

    T.S

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Java Stack problems, return of peek().

    peek won't return what I expected.
    Please explain. What does it do? What do you expect it to do?
    Can you post a small complete program that compiles and executes and shows your problem?

  3. #3
    Tom_Snake is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Java Stack problems, return of peek().

    I can't really post the program, perhaps bits and bobs, because it's pretty big by now.
    This is what I want: A class called Player can store some info, one being the rooms the player has visited. I want this to be stored in a Stack so that I can issue a method called back, which pops the stack and then set the current room, also stored in Player, to the top element of the stack.

    something like this (heaps of code omitted):
    Java Code:
    import java.util.Stack;
    public class Player{
    private Room currentRoom;
    private Stack previousRoom;
    }
    
     public Player(Room start){
    
            // initialise instance variables
            this.currentRoom = start;
            previousRoom = new Stack();
        }
    
     /**
         * push the stack
         */
        public void pushStack(Room room){
            previousRoom.push(room);
        }
        
        /**
         * pop the stack
         */
        public void popStack(){
            if(!stackEmpty()){
                previousRoom.pop();
            }
        }
        /**
         * peek the stack
         */
        public Room peekStack(){
            if(!stackEmpty()){
                return previousRoom.peek();
            }
        }
    
    I've cropped the code, but the main concept is there. Why isn't previousRoom.peek() returning a Room for me to play with?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Java Stack problems, return of peek().

    Its hard to compile and test "concepts".
    If you make a SSCCE program that we can test, that would help.

    The posted code has logic flaws: peekStack does not always return something.

  5. #5
    Tom_Snake is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Java Stack problems, return of peek().

    Reading about SSCCE I see that my response is ignorant, and I apologize. I'll try and make a SSCCE, but that might take a while being my first qualified question.
    Could you expand on the peekStack logic flaw?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Java Stack problems, return of peek().

    peekStack does not always return something.
    The compiler will tell you if you try to compile that code.

  7. #7
    Tom_Snake is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Java Stack problems, return of peek().

    The compiler tells me that peekStack method has incompatible types, but surely both are of type Room? Or is this the logic flaw acting up?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Java Stack problems, return of peek().

    What does the peek() method return? Is it a Room or an ???

    Is that the only error?

    I just noticed this in your first post: The console tells me ...
    Now I understand that you meant to say the compiler gives an error message....

    When you get compiler errors, you should copy and paste here the full text of the message.
    Last edited by Norm; 12-01-2011 at 05:20 PM.

  9. #9
    Tom_Snake is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Java Stack problems, return of peek().

    Actually, that's what I was hoping you could tell me. The Java API tells me an Object, I figured it would be of the same type that I entered in the first place.

    Yes, compiler of course. The full message is 'incompatible types', pointing to the first menthods I posted. Coding in BlueJ if that matters. Thats all the compiler gives me.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Java Stack problems, return of peek().

    Do you know about casting? If the type of the object is Room, then you can cast the Object it returns to Room.

  11. #11
    Tom_Snake is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Java Stack problems, return of peek().

    I do think I know about casting, as in: String s = "foo"; or using a parameter of type String, like: String param = "bar"; String s = param;
    But the problem here is that my casting attempts won't work. So, is the return object from previousRoom.peek() not a Room, even though I push an object of type Room into the stack previousRoom?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Java Stack problems, return of peek().

    my casting attempts won't work.
    Please explain. Please post the error messages or show what the problem is.

    Have you looked at using Generics? You can define the Stack as holding only Room objects and you don't have to cast.

  13. #13
    Tom_Snake is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Java Stack problems, return of peek().

    Well, I've already posted my methods and the attempts in the first post, the two similar methods called peekStack. None of those two implementations would work.

    So, I've tried this:

    Java Code:
    /**
     * peek at the top of the stack
     */
    public void peekStack(){
        if(!stackEmpty()){
            currentRoom = previousRoom.peek();
        }
    }
    And this:

    Java Code:
    /**
     * peek at the top of the stack
     */
    public Room peekStack(){
        if(!stackEmpty()){
            return previousRoom.peek();
        }
    }
    But both yields in the compiler telling me that the return type I specified and what is returned is not the same, i.e. 'incompatible types' That is the error message in its full.

    No, I have not tried generics, where do I define the Stacks content? Couldn't see anything in the API

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Java Stack problems, return of peek().

    Couldn't see anything in the API
    Look at the top of the API doc: Class Stack<E>
    The <E> is the generics mod to the Stack class.

    None of those two implementations would work.
    Please explain what "wouldn't work" means.
    Does the code compile? If not post the errors.
    Does the code execute with errors? Post the errors.
    Does the code return the wrong results? Post what it does return and explain why that is wrong and say what it should be.

    I do think I know about casting
    I'm not sure you do. Better check you text or tutorial about casting.
    Or google it.
    Last edited by Norm; 12-01-2011 at 07:47 PM.

  15. #15
    Tom_Snake is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Java Stack problems, return of peek().

    You were right, I didn't know what casting was. I have seen it before, never utilized it though. Anyway, by casting (Room) after the equal sign everything worked out fine. Thank you for your help.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Java Stack problems, return of peek().

    Also look into Generics. It will remove the need to cast for most of the collection classes.

  17. #17
    Tom_Snake is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Java Stack problems, return of peek().

    I implemented that too, but that renders the casting redundant, correct? The reason I couldn't find generics in the first place was because I looked at this: old API. Silly beginner mistakes, I really appreciate you patience

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Java Stack problems, return of peek().

    Always more to learn. And they keep changing it.

Similar Threads

  1. java stack RPN calculator
    By ali1 in forum New To Java
    Replies: 0
    Last Post: 10-30-2011, 10:04 PM
  2. Java Stack and Queue
    By jeanjiang in forum New To Java
    Replies: 3
    Last Post: 04-25-2011, 03:19 PM
  3. Java Stack Overflow?
    By fullmetaljacket in forum New To Java
    Replies: 0
    Last Post: 05-19-2009, 08:49 PM
  4. Stack push/pop/peek operations
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-29-2008, 10:03 AM
  5. Using java.util.Stack
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-20-2007, 06:17 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
  •