Home arrow static arrow Java Programming [Archive] - Ending an Endless Loop
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Ending an Endless Loop
This topic has 5 replies on 1 page.

Posts:14
Registered: 2/19/04
Ending an Endless Loop  
Jun 16, 2004 10:46 AM



 
		while (! source.nextTokenIs('}'))		{			dec = new ClassMemberDeclaration();          			dec.compile(source);		}


basically this question is regarding the above loop. This is a while loop that is looking for curly braces
 (i.e. '}' ) 
. But if there are no curly braces of that kind in the file that i am reading then this while loop becomes an endless loop. hence is there any way of timing this method, and after certain time (say 20 seconds the while loop is aborted and a message is displayed saying that the closing curly brace was not found????

please ignore the body of the method as it was just put there so that you could understand what i was on about.

please do reply, thank you
best regards

alpha
 

Posts:31,095
Registered: 4/30/99
Re: Ending an Endless Loop  
Jun 16, 2004 10:54 AM (reply 1 of 5)



 
That's probably the wrong solution. If you are really processing a file, as your post but not your code suggests, then exit the loop when you find a '}' or when you reach the end of the file.

PC²
 

Posts:3,534
Registered: 9/30/99
Re: Ending an Endless Loop  
Jun 16, 2004 10:56 AM (reply 2 of 5)



 
if you are reading from a file you should be doing a read someplace, since you are not asking why things are blowing up, I'll assume that you handle the end of file appropriately. When you find the end of file set a global flag (global with respect to your while loop and your file reading routines. Add the clause to your conditional to check for the flag.

  while ((! source.nextTokenIS('}')) && (! Flag))    { loop logic here    }


You'll exit then when you set your Flag.
 

Posts:10,972
Registered: 10/23/03
Re: Ending an Endless Loop  
Jun 16, 2004 11:10 AM (reply 3 of 5)



 
Endless loop? See this thread: http://forum.java.sun.com/thread.jsp?forum=31&thread=531135&start=0&range=15
 

Posts:18
Registered: 8/12/03
Re: Ending an Endless Loop  
Jun 16, 2004 11:13 AM (reply 4 of 5)



 
Endless loop? See this thread:
http://forum.java.sun.com/thread.jsp?forum=31&thread=53
135&start=0&range=15

Flag=1

There, just saved so many people endless hours ;)

 

Posts:14
Registered: 2/19/04
Re: Ending an Endless Loop  
Jun 16, 2004 11:25 AM (reply 5 of 5)



 

Thanks guys, i apreciate your help.

I have managed to work upon the flag advice and did it.
thanks to everyone

best regards

alpha
 
This topic has 5 replies on 1 page.