This sounds like a logic problem. Your code probably doesn't follow your algorithm correctly.
If you know how your algorithm works, you should be able to look at the printed output from printlns of...