This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
A good approach to understanding such problems is to run the code. You can also put additional print statements in to check the values of the variables i and j. There is also the technique of using a symbolic debugger such as that provided by the free Eclipse IDE. And there is also the java debugger jdb.