Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with code

  1. #1
    Junior Member pyler's Avatar
    Join Date
    Sep 2012
    Posts
    23
    My Mood
    Busy
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Problem with code

    I have a generic LinkedStack with a Node class and a number of methods
    LinkedStack<T>{
    class Node<T>{}//
    void push(T item){}//
    void push(FancyStack<T> s){}//
    T pop(){}
    boolean isEmpty(LinkedStack<T> stk){}
    T peek(){}//
    LinkedStack<T> rev(){}//reverses elements in this stack
    }

    When I create a new LinkedStack, push items onto it, and print the stack, I get memory addresses and not items in the stack.
    What am I doing wrong?
    Here's my code


    import java.util.NoSuchElementException;
    //import java.util.Stack;
    public class LinkedStack<T>{
    	public class Node<T>{
    		private T data;
    		private Node<T> link;
    		public Node(T data,Node<T>link){
    			this.data=data;
    			this.link=link;
    		}//Node constructor
    	public void SetData(T info){data =info;}
     
    	public T getData(){return data;}
     
    	public void setLink(Node<T> newLink){link=newLink;}
     
    	public Node<T> getLink(){return link;}
    }//end of node
     
    private Node<T> high=null;//
    public void push(T thing){//this push
    	high=new Node<T>(thing,high);
    	}
     
    public void push(LinkedStack<T> stk){
    	final int MAX= stk.size();
    	for(int i=0;i<MAX;i++){
    		this.push(stk.pop());}}//
     
    public T peek(){
    	if(high==null){throw new NoSuchElementException();}
    	else
    		return high.getData();
    	}
     
    public T pop(){
    		T thing=peek();
    		high=high.link;
    		return thing;
    	}
     
    public boolean isEmpty(LinkedStack<T> stack){
    	return(high==null);
    }
     
    int hesabu=0;
    public int size(){
    	hesabu++;
    	return hesabu;
    }
     
    public LinkedStack<T> rev(){
    	LinkedStack<T> revStk= new LinkedStack<T>();
    	final int LEN= this.size();
    	for(int j=0;j<LEN;j++){
    		revStk.push(this.pop());}
    	return revStk;//end of method
    		} 
    }//end of LinkedStack

    So for example when I create a new LinkedStack object
    as in;
    LinkedStack<String> stk=new LinkedStack<String>();
    and push things onto it as in
    stk.push("Me");
    stk.push("Me too");
    stk.push("Me me me");
    stk.push("Me myself and I");
    When I print out the stack like
    System.out.println(stk)
    I get an erorr of the sort
    LinkedStack@56r3t333

    I'm confused so any help is appreciated


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Problem with code

    The output you see is from the toString method of Object class. Does your LinkedStack class have its own toString method?
    Improving the world one idiot at a time!

  3. #3
    Junior Member pyler's Avatar
    Join Date
    Sep 2012
    Posts
    23
    My Mood
    Busy
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: Problem with code

    Quote Originally Posted by Junky View Post
    The output you see is from the toString method of Object class. Does your LinkedStack class have its own toString method?
    Thanks for that

Similar Threads

  1. [SOLVED] My code has a problem ;)
    By einar123 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: August 5th, 2013, 03:52 PM
  2. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  3. I have a problem with my code I think
    By byrne in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 12th, 2011, 04:18 AM
  4. Problem with code
    By lichad3 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 22nd, 2011, 06:40 PM
  5. problem in my code
    By wannabe in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2010, 07:53 AM

Tags for this Thread