This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30138
    
150

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: 38075
    
  22
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: While Loop Not Ending
 
Similar Threads
Base Converter
Explanation for Code
Explanation for Code : Parsing XML using Java
Threads
Generate new generation binary // need help