Put some System.out.println()s in your for loops that print out the values of the loop indexes (i and j) and also the contents of the StringBuffers name1 and name2 so that you can see how they change...