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 2 of 2

Thread: Some of my methods don't run properly

  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 Some of my methods don't run properly

    The reverse stack method in my LinkedStack class is supposed to return a reversed copy of the stack object that uses it. So when I populate somestack with strings as below, and do somestack.reversed(); System.out.println(somestack); should print a reversed version of somestack. Instead all I get is an empty stack when I run the code below.

    LinkedStack<String> somestack=new LinkedStack<String>();
    somestack.push("Decide");
    somestack.push("Heart");
    somestack.push("Fears");
    somestack.push(Strength"");
    somestack.push("Potluck");
    System.out.println("Stack was"+somestack);
    somestack.reversed();
    System.out.println("Stack is reversed: "+somestack);

    Output
    Potluck
    Strength
    Fears
    Heart
    Decide
    Stack is reversed:

    Reversed should print whole stack beginning with Decide
    Can any one find an error in my code?
    import java.util.NoSuchElementException;
    //import java.util.Stack;
    public class LinkedStack<T>{
    	@SuppressWarnings("hiding")
    	public class Node<>{
    		private T info;
    		private Node<T> next;
    		public Node(T info,Node<T>next){
    			this.info=info;
    			this.next=next;
    		}//Node constructor
    	public void setInfo(T info){this.info =info;}
     
    	public T getInfo(){return info;}
     
    	public void setNext(Node<T> newNext){this.next=next;}
     
    	public Node<T> getNext(){return next;}
    }//end of node
     
    private Node<T> top=null;//whenever newFS created top=null
     
    public void push(T element){//this push
    	top=new Node<T>(element,top);
    	}
     
    public void push(LinkedStack<T> stack){
    	//int SIZE = stack.size();
    	while(!(stack.empty()))//for(int i=0;i<SIZE;i++){
    		this.push(stack.pop());}}
     
    public T peek(){
    	if(top==null){throw new NoSuchElementException();}
    	else
    	     return top.getInfo();
    	}
     
    public T pop(){
    		T element=peek();
    		top=top.next;
    		return element;
    	}
     
    public boolean isEmpty(LinkedStack<T> s){
    	return(top==null);
    }
     
    int count=0;
    public int size(){
    	count++;
    	return count;
    }
     
     
    public LinkedStack<T> reversed()
    {
    	LinkedStack<T> revertStack = new LinkedStack<T>();
    	//int LENGTH = this.size();
    	while(!(this.empty()))//for(int j=0;j<LENGTH;j++)
              {
                  revertStack.push(pop());
              }
    	return this;//end of method
    }
     
    //Returns a string representation of this stack
    public String toString(){
    	String result="";
    	Node<T> current=top;//set the current node to top
    	while(current !=null){//while link isn't null
    		result=result+(current.getInfo()).toString()+"\n";
    		current=current.getNext();
    	}
    	return result;//return result
    }
    }//end of LinkedStack


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Some of my methods don't run properly

    Your code contains quite a number of syntax errors, such as unclosed strings, braces and typed arguments. Then you also keep refering to this.empty() which doesn't exist. So instead of working 1 by 1 for hours with you, heres a cleaned up version of your code, which works fine. NOTE: no logic was changed, only syntax.

    import java.util.NoSuchElementException;
     
    //import java.util.Stack;
    public class LinkedStack<T> {
     
    	public static void main(String[] args){
    		LinkedStack<String> somestack=new LinkedStack<String>();
    		somestack.push("Decide");
    		somestack.push("Heart");
    		somestack.push("Fears");
    		somestack.push("Strength");
    		somestack.push("Potluck");
    		System.out.println("Stack is: \n"+somestack);
    		System.out.println("Stack is reversed: "+somestack.reversed());
    	}
     
    	@SuppressWarnings("hiding")
    	public class Node<T> {
    		private T info;
    		private Node<T> next;
     
    		public Node(T info, Node<T> next) {
    			this.info = info;
    			this.next = next;
    		}// Node constructor
     
    		public void setInfo(T info) {
    			this.info = info;
    		}
     
    		public T getInfo() {
    			return info;
    		}
     
    		public void setNext(Node<T> newNext) {
    			this.next = next;
    		}
     
    		public Node<T> getNext() {
    			return next;
    		}
    	}// end of node
     
    	private Node<T> top = null;// whenever newFS created top=null
     
    	public void push(T element) {// this push
    		top = new Node<T>(element, top);
    	}
     
    	public void push(LinkedStack<T> stack) {
    		// int SIZE = stack.size();
    		while (!(stack.isEmpty(this))) {
    			this.push(stack.pop());
    		}
    	}
     
    	public T peek() {
    		if (top == null) {
    			throw new NoSuchElementException();
    		} else
    			return top.getInfo();
    	}
     
    	public T pop() {
    		T element = peek();
    		top = top.next;
    		return element;
    	}
     
    	public boolean isEmpty(LinkedStack<T> s) {
    		return (top == null);
    	}
     
    	int count = 0;
     
    	public int size() {
    		count++;
    		return count;
    	}
     
    	public LinkedStack<T> reversed() {
    		LinkedStack<T> revertStack = new LinkedStack<T>();
    		// int LENGTH = this.size();
    		while (!(this.isEmpty(this)))// for(int j=0;j<LENGTH;j++)
    		{
    			revertStack.push(this.pop());
    		}
    		return revertStack;// end of method
    	}
     
    	// Returns a string representation of this stack
    	public String toString() {
    		String result = "";
    		Node<T> current = top;// set the current node to top
    		while (current != null) {// while link isn't null
    			result = result + (current.getInfo()).toString() + "\n";
    			current = current.getNext();
    		}
    		return result;// return result
    	}
    }// end of LinkedStack
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. GUI won't run properly once panel is added
    By mwardjava92 in forum AWT / Java Swing
    Replies: 4
    Last Post: February 23rd, 2013, 01:41 PM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. Don't understand void methods, need help!
    By alex067 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 9th, 2012, 07:02 AM
  4. First Go: I don't think my code is arranged properly.
    By 05mooree in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 12th, 2012, 07:20 AM
  5. Code compiles fine but doesn't run Properly
    By nadirkhan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 9th, 2011, 12:46 PM

Tags for this Thread