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

Thread: Can't seem to understand why my while loop doesn't exit!

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can't seem to understand why my while loop doesn't exit!

    I'm gonna past a small piece of code that reads data from a socket server coming from flash, and then sends it back to a client, this is the code:

        public void run() 
    	{
            try 
            {
                char charBuffer[] = new char[1];
     
                while(in.read(charBuffer,0,1) != -1) 
    	    {
                  StringBuffer stringBuffer = new StringBuffer(8192);
     
                  while(charBuffer[0] != '\0') 
    		{
    		System.out.println("loop 2");
                    stringBuffer.append(charBuffer[0]);
                    in.read(charBuffer, 0 ,1);
                    }
                    System.out.println("Sending message");
                    server.sendMessage(stringBuffer.toString(),this);
                }
            } 
    	catch(IOException ioe) 
    	{
     
            } 
           finally 
    	{
                killClient();
            }
        }

    The first while loop checks if it's possible to read data, but it will return false if there is none right? That would mean that the while loop will not execute and it will kill the client, but that doesn't happen until I actually disconnect.

    I added a lot of System.out.println statements, what happens is the following:

    When client connects, the try block starts
    When I send a message, the first while loop executes one time, then the second loop executes the amount of times equal to the amount of characters sent, then it sends the message, and that's where it ends, it never leaves the first while loop until I disconnect! And my brain is killing me! I can't figure out why it would do that...

    Can someone help me with this small but annoying thing I have going on? I just can't seem to find the logic why it doesn't execute the finally block


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: Can't seem to understand why my while loop doesn't exit!

    What evidence do you have that it is getting hung up in the outer while loop? Put some print statements in the catch and the finally blocks (before the kill method) and see what happens. Something odd could be occurring.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't seem to understand why my while loop doesn't exit!

    Quote Originally Posted by aussiemcgr View Post
    What evidence do you have that it is getting hung up in the outer while loop? Put some print statements in the catch and the finally blocks (before the kill method) and see what happens. Something odd could be occurring.

    Putting a print statement in the catch and finally block doesn't do anything for me, it looks like it's working like it should, but I still don't know why the loop keeps looping (I would assume you would need while(true) for that...)

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: Can't seem to understand why my while loop doesn't exit!

    ok, so is it still looping the outside loop or is it just stopping after the second loop?

    If it is still looping the outside loop, I would advise researching possible cases where the in.read(charBuffer,0,1) call will return a number other than -1, but provide data that you couldn't use for some reason. Or check for situations where the statement might repeat the same operation for some reason.

    If it is stopping after the second loop, we need to find exactly where it is stopping.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't seem to understand why my while loop doesn't exit!

    Ok, I changed my code to this to this:

     public void run() 
    {
     
    	System.out.println("Starting run()");
     
            try 
    	{
                    char charBuffer[] = new char[1];
    	        while(true && (in.read(charBuffer, 0 ,1)) != -1)
    		{
    			StringBuffer stringBuffer = new StringBuffer(8192);
    			stringBuffer.append(charBuffer[0]);
     
    			while(charBuffer[0] != '\0') 
    			{ 
                              in.read(charBuffer, 0 ,1);
    		          stringBuffer.append(charBuffer[0]);
                           }
     
    			server.sendMessage(stringBuffer.toString(),this);
     
    		}
            }
    		catch(IOException ioe) 
    		{
    			System.out.println(ioe);
                     }
                     finally 
    		{
                           killClient();
                     }
    }


    I actually put a print statement right above the creation of the StringBuffer so I could see when the while loop would start, and I put a print statement right under the second while statement (above the in.read(charBuffer, 0 ,1); )

    When I send a message, the first loop executes ONCE, then the second loop executes UNTIL it finds the character '\0' , then it just sends the message. this is weird, since I would expect either:

    The finally block executes because the while loop is false

    or

    The first loop keeps looping (this isn't the case, because I only get one printed message)
    Last edited by vortexnl; February 11th, 2011 at 10:40 AM.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,334
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Can't seem to understand why my while loop doesn't exit!

    Is your InputSteam blocking? As per the API for InputStream:
    This method blocks until input data is available, end of file is detected, or an exception is thrown.

  7. #7
    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: Can't seem to understand why my while loop doesn't exit!

    This thread has been cross posted here:
    Can't seem to understand why my while loop doesn't exit! - Java Forums

    Although cross posting is allowed, for everyone's benefit, please read:
    Java Programming Forums Cross Posting Rules
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. I can''t understand this error
    By ragingdemon in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 8th, 2011, 05:07 PM
  2. 2 errors I can't understand
    By Brock in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 26th, 2010, 11:56 PM
  3. Jar self delete on exit?
    By KiwiProg in forum Java Theory & Questions
    Replies: 1
    Last Post: December 19th, 2010, 01:54 AM
  4. Dont understand what to write.. :/
    By johnwalker in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 08:31 PM
  5. While Loop Exit with String
    By Zaroth in forum Loops & Control Statements
    Replies: 8
    Last Post: November 9th, 2009, 03:20 PM