Results 1 to 3 of 3
  1. #1
    ile4 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default Stuck while loop

    Having a little trouble with my while loop, as it runs but gets stuck.
    Basically, I have a list of integers which are in order of size, and several or many could be the same, and I want to find all the ones that are the same. So I am taking the first number, comparing it with the second, then the third, until it finds one that is different, and then moving on.

    I've run some debugging tests and my code is finding the first 2 integers that are the same, moving onto my second while loop, finding that the second and third numbers are not the same, but for some reason isn't moving on. It doesn't go through the second while loop if b is null or i isn't equal to b, but then it goes back to the top of the while loop and goes round and round in the same way.

    This is my code:

    Java Code:
    Int i = startingInteger; //i is the first integer in my list
    
            while (i.next != null) //while there is a second integer
            {
                if(i == i.next) //if first and second values are the same
                {
                    int b = i.next; 
                    while (b != null && (i == b))
                    {
                        b = b.next;
                    }
                }
                else
                {
                    i = i.next; //move on to the next word    
                }
            }
    I was certain my code is correct so I am thinking I have made a small mistake which is causing the loop to get stuck. Does anybody have any suggestions?
    Thank you
    Last edited by ile4; 12-06-2010 at 08:09 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Does your code compile? You seem to be trying to get a field from a primitive, an int.

    I suggest you create and post an SSCCE, one that is small, compiles, and shows your error. Do this and you'll likely get faster, better help.

  3. #3
    ile4 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    Sorry, my mistake. I meant to type Int because I have created my own Int class that stores integer values and these are stored in a list.
    I have just realised my own problem as I edited my post, that I have used 'else' when I shouldn't have because it should set i to i.next when the while loop is not satisfied.

    So I have solved my own problem for once, sorry to waste your time.

Similar Threads

  1. I'm stuck help!!!
    By nobody58 in forum Advanced Java
    Replies: 2
    Last Post: 03-18-2010, 02:52 PM
  2. Stuck in sea
    By programmer_007 in forum JDBC
    Replies: 1
    Last Post: 09-17-2009, 04:00 AM
  3. Im on my last lab!!!! And im stuck...:(
    By clanboru15 in forum New To Java
    Replies: 5
    Last Post: 03-13-2009, 01:44 AM
  4. really stuck now..
    By shongo in forum Advanced Java
    Replies: 2
    Last Post: 11-09-2008, 02:56 AM
  5. Stuck in need of help!
    By Zombie_Leg! in forum New To Java
    Replies: 1
    Last Post: 09-23-2008, 02:22 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
  •