File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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

ignore after an iteration in a while loop

ernest mcdermott

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

Joined: Jul 08, 2003
Posts: 24199

"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

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:
subject: ignore after an iteration in a while loop
It's not a secret anymore!