• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ignore after an iteration in a while loop

 
ernest mcdermott
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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?
 
ernest mcdermott
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic