jQuery in Action, 3rd edition
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

While Loop Not Ending

Andrew Ceronoski

Joined: Apr 21, 2009
Posts: 16

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

Joined: May 26, 2003
Posts: 33102

How about changing the loop to:

That way you get the check twice.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
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

Joined: Jul 10, 2009
Posts: 11

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:
anilkumar patil

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.

I agree. Here's the link: http://aspose.com/file-tools
subject: While Loop Not Ending
It's not a secret anymore!