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

Thread: While loop stops.

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default While loop stops.

    I'm making an instant messenger in java and using a thread to check if there are any received messages. But for some reason it only loops through the while loop in my run method once. I put a print line at the beginning of the loop and it a print line before the only statement where it can terminate itself and I don't see either one of those print statements (except I see the fist one, once). I also go no error messages and my application isn't frozen. Why does it only loop once?

    By the way. I know it's messenger, not messanger. But it's only a test application so I'm not going to sniff out all my variables over one misspelling.

    	BufferedReader in;
    	String incomingMessage;
    	Thread t;
     
    	public MessageReciever(BufferedReader i)
    	{
    		in = i;
    		t = new Thread(this);
    		t.start();
    	}
     
            public void run()
    	{
    		try
    		{
    			while(true)
    			{
    				System.out.println("1");
    				try
    				{
    					if((incomingMessage = in.readLine()) != null)
    						MessangerDisplay.append(incomingMessage);
    				}
    				catch(IOException e)
    				{
    					System.out.println("x");
    					MessangerDisplay.append("The user has dropped.");
    					stop();
    				}
     
    				t.sleep(50);
    			}
    		}
    		catch(InterruptedException e2)
    		{
    			MessangerDisplay.append("Thread interrupted.");
    		}
    	}

    Last edited by MrFish; October 16th, 2010 at 09:37 AM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,554
    Thanks
    24
    Thanked 304 Times in 286 Posts

    Default Re: While loop stops.

    Add some println statements so we can figure out where exactly something is occurring.

    Put a statement directly before and after the thread sleep. Also put some statements inside the try statement. 1 before the if statement. 1 before the call in the if statement. 1 after the call in the if statement. 1 after the if statement. Also, put a statement in the catch statement where you catch the InterruptedException. Lastly, put a statement before and after the while loop.

    Basically, since we are having an issue, we want to figure out exactly where that issue is. Perhaps the loop is only running once because it reaches a point where it gets stuck. If so, we want to find out where that is. When you are having a issue that you cannot track down, you need to figure out the general area of where it is happening. Print statements can be your best friend here. However, you should remember to remove them when they are not relevant to the issue at hand and you have to be careful about overloading, as they will slow down your program and will sometimes cause a crash. When you are initially debugging a situation like this, it doesnt hurt to overload in print statements. Doing so will narrow down the locations for us to look. When we rule out a specific area (especially in loops), it doesn't hurt to at least comment out those print statements for the time being.

    When I debug with print statements, I always make each printout unique so I can track them back. I usually do things like: System.out.println("Check 1"); System.out.println("Check 2");...ect. Doing that can allow me to easily find the location while just reading the printouts and not even looking at the code. I can then go back and look at the code to find where that check occurred. Things to worry about are checks running repeating by itself alot and not reaching the final check, where ever you expect that to be.

    Tell us what you get for your output and we can work from there.
    Last edited by aussiemcgr; October 16th, 2010 at 09:14 AM.
    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
    Oct 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: While loop stops.

    Ok, I've done that and found that this is the line that pauses it.

    incomingMessage = in.readLine()

    If I comment it out the loop runs. I've printed in.toString() and it is my BufferedReader object.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,554
    Thanks
    24
    Thanked 304 Times in 286 Posts

    Default Re: While loop stops.

    Quote Originally Posted by MrFish View Post
    Ok, I've done that and found that this is the line that pauses it.

    incomingMessage = in.readLine()

    If I comment it out the loop runs. I've printed in.toString() and it is my BufferedReader object.
    Oh, wow, I can't believe I didn't realize. So obvious.

    The BufferedReader reading methods wait for the user to press Enter (I think) till it reads the line. Basically, it is designed to wait until an input occurs.
    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. The Following User Says Thank You to aussiemcgr For This Useful Post:

    MrFish (October 16th, 2010)

  6. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: While loop stops.

    Ah! Now I can get it to work. Thanks for your help

Similar Threads

  1. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  2. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 04:27 PM
  3. loop or what
    By silverspoon34 in forum Loops & Control Statements
    Replies: 5
    Last Post: November 19th, 2009, 01:10 PM
  4. Check difference between no. of stops, calculate cost
    By JavaStudent23 in forum Java Theory & Questions
    Replies: 4
    Last Post: November 17th, 2009, 02:29 AM
  5. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM