aspose file tools*
The moose likes Java in General and the fly likes ignore after an iteration in a while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ignore after an iteration in a while loop" Watch "ignore after an iteration in a while loop" New topic
Author

ignore after an iteration in a while loop

ernest mcdermott
Greenhorn

Joined: Mar 05, 2007
Posts: 28
hi everyone,
i currently have code which allows me to take the name of a place from an array, then find the next smallest distance away from it (found in a 2d array) and then find the place that this leads to. I have this on loop however im having problems when i try to ensure that no cycles are made, aka that the program never returns to the same place even if it is the next smallest distance.
Below is my method for this, although it compiles fine can anyone tell me if they think it will work. My problem is with the method calls of this from another file but id first like to try and establish if this is ok to start with.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

"if (igNode == n[l])" will never, never be true, because igNode is a brand new String, created just before, that can't possibly be the value of n[1].

If you mean to check whether n[1] is a zero-length String, or more generally, has a specific String value, then you must use the equals() method, not "==", like "if (ifNode.equals(n[1]))..."

Now, with that out of the way, the statement controlled by the "if" is the empty set of braces right after -- i.e., you've written "if (impossible thing) then do nothing." The next two statements that assign values to your two-element array are always executed. Nowhere do I see anything that looks like searching for cycles.

If you've got trees of connected nodes with names and links, shouldn't you have a "Node" class with a "name" property, some "next" and "prev" or "right" and "left" properties, and various useful methods for linking, unlinking, searching, etc defined inside?


[Jess in Action][AskingGoodQuestions]
ernest mcdermott
Greenhorn

Joined: Mar 05, 2007
Posts: 28
Thanks Ernest,
i took your suggestion for the if statement. all i want it to do however is just to skip if igNode is equal to n[l]. this is my means for checking that no cycle occurs.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ignore after an iteration in a while loop