aspose file tools*
The moose likes Beginning Java and the fly likes While Loop Not Ending Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "While Loop Not Ending" Watch "While Loop Not Ending" New topic
Author

While Loop Not Ending

Andrew Ceronoski
Greenhorn

Joined: Apr 21, 2009
Posts: 16
Hello:

For the code below; when I run the code as part of a method I made, it always runs through the for loop before ending.

What i am trying to do is: When the code has to use this section to choose a new step matching a branch value, it must go through the rest of the steps. As soon as it reaches a good step, i want to set s=1, and stop the while loop, ending the for loop and therefore ending this section of code. I tried putting the while loop inside the for loop, but then the program hangs in the for loop. Any help would be most appreciated!
Thank you.

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30938
    
158

Andrew,
How about changing the loop to:

That way you get the check twice.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Why are you using a number to check whether the loop has finished? You should have a boolean called valueFound or similar. Your code style suggests you have come from C to Java; you use numbers for true and false in C, but not in Java or C#.
Frank Kellinghusen
Greenhorn

Joined: Jul 10, 2009
Posts: 11
Andrew,

it is hard to help you, because from your example I can't follow what you try to accomplish.

You might want to look at the continue or break statements, which let you exit the current iteration or the whole loop itself. The following link also explains labeled and unlabeled loops:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/branch.html
anilkumar patil
Greenhorn

Joined: Jul 14, 2009
Posts: 5
Andrew , there are lot of issues in this code.
1. I assume ma is of type String and if this is String then you should not do ma =="ALL". ma and "ALL" will never be same as in case of Strings == will check for the object reference and not the value.
2. You are not considering the case when ma does not contain 'fire' and ma is not equal to "ALL", so that means the variable S will never be set to 1 and even if the for loop ends after some pre defined iterations you are again starting the for loop as the S is still 0 and as you are ierating over the same previous data set which does not match your condition this will keep looping.

Please modify your conditions to consider all the scenarios and ideally you should always have a else for the if condition dont just end with else if.

thanks
 
wood burning stoves
 
subject: While Loop Not Ending