Results 1 to 5 of 5
  1. #1
    Aaron5466 is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default while loop problem

    Below is a class, ListNode2, with a method named Display that contains a while loop. My other class, Stack, creates a ListNode2 object LN2 then calls the Display method. (I apologize if Iím misusing Object Oriented Programming terminology.) For some reason, when I run them, the while loop gets stuck, Iím pretty sure at the condition. The output is ďhead3,Ē and the program keeps running, stuck in an infinite loop. After my Stack class, Iíve pasted a modified version of the Display method where the while loop uses a conditional with an int instead of a ListNode2. Now the output is
    head3In while loopIn getItem
    -->1Just after while loop
    


    Just before displayhead3In while loopIn getItem
    -->100Just after while loop

    The program is getting into the body of the while loop. Why isnít it doing so with the original Display method? At the very end, I'm pasting my class StackMain.java that I use as for the main method.

    Java Code:
    // ListNode2.java


    class ListNode2

    {
    
   int data;
    
   private Object item;

       private ListNode2 next;
    

   public ListNode2 (int n)
   
       {
    
      ListNode2 head = Create(n);
          Display(head);

       }
    
    
   
public ListNode2 (Object newItem)
       {
    
      item = newItem;

          next = null;
    
   }
    
    

   public ListNode2 (Object newItem, ListNode2 nextNode)
    
   {
    
      item = newItem;//left most part of new head

          next = nextNode;//the rest of the new ListNode2, and the entire previous head
    
   }
    
    
   public Object getItem()

       {
    
      System.out.print("In getItem\n");
    
      return item;

       }

    
     
  public void setItem (Object newItem)
    
   {
    
      item = newItem;
    
   }
    
    

   public ListNode2 getNext()

       {
    
      return next;
    
   }
    
    

   public void setNext (ListNode2 nextNode)
    
   {

          next = nextNode;
    
   }
    
    

   public static ListNode2 Create (int n)// creates a linked list of length n, n>0, with data counting down from n to 1.
   
       {
    
   ListNode2 head = new ListNode2(n, null);
    
      
      while (n>1)

          {
    
         n = n-1;
             head = new ListNode2 (n, head);
    
      }

          return head;
    
   }
    
    

   public static void Display (ListNode2 head)

       {
    
      System.out.print ("head3");
     
      while (head != null);
    
      {
    
         System.out.print ("In while loop");
    
         System.out.print("-->" + head.getItem());
    
         head = head.getNext();
    
      }
    
      System.out.print ("Just after while loop");
    
      System.out.print("\n\n");
    
   }
    
}
    
    // Stack.java

    // written by Aaron Poley
    

// Uses ListNode.
    java
import java.io.*;


    
    class Stack
    
{
    
   private ListNode2 LN;
    
   private int size;
    
   public Object itemObject = 100;
    

   public Stack(int size)//constructor.
    
   {
    
      ListNode2 LN = new ListNode2(size);
    
      ListNode2 LN2 = new ListNode2(itemObject, LN);
    
      System.out.print ("Just before display");
    
      LN2.Display(LN2);

       }
    

}
    //end Stack.java
    
       public static void Display (ListNode2 head)

       {
    
      System.out.print ("head3"); 

    //    while (head != null);
          int x1 = 1;

          while (x1 != 0)

          {

             System.out.print ("In while loop");
    
         System.out.print("-->" + head.getItem());

             head = head.getNext();
    
         x1 = x1-1;
    
      }

          System.out.print ("Just after while loop");
    
      System.out.print("\n\n");
    
   }
    
    // StackMain.java
    
    import java.io.*;
    
    class StackMain
    {
    	public static void main (String args[])
    	{
    		Stack Param = new Stack(5);
    		//ListNose2.Create(13)
    		// ??? Is there a way to call the Create method from outside the ListNode.java file?
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,232
    Rep Power
    19

    Default Re: while loop problem

    This is your third post, and the third thread you have started. You didn't have the courtesy to reply to the two responses on each of your earlier threads.

    Do you really think the forum members will continue to toss out tidbits of advice into a vacuum forever?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: while loop problem

    It is a typical syntax error: check your "while" line in the code at the end and then despair... it is just a simple ugly character wrong there... ;)
    Last edited by Sierra; 04-12-2012 at 05:13 PM.

  4. #4
    Aaron5466 is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: while loop problem

    Thanks, Sierra. I will try that. And I apologize to db for not using better manners.

  5. #5
    Aaron5466 is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: while loop problem

    It solved the immediate problem. Thanks, Sierra!

Similar Threads

  1. For loop problem
    By dougie1809 in forum New To Java
    Replies: 9
    Last Post: 03-25-2012, 05:51 PM
  2. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  3. Problem with the while loop
    By molokomesto in forum New To Java
    Replies: 4
    Last Post: 02-14-2010, 05:01 PM
  4. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  5. Help with a loop-like problem
    By Jnoobs in forum New To Java
    Replies: 1
    Last Post: 10-14-2009, 01:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •