Reputation:
I've been trying to print out a Map to a string but at the same time adding additional item in the process. This is what I'm trying to achieve this: "---\n---\n---\n" But some how it returns like this: "\n---\n---\n---" Anyone have any idea how to fix this?
//this is my TreeMap
{
board.put(0,"-");
board.put(1,"-");
board.put(2,"-");
board.put(3,"-");
board.put(4,"-");
board.put(5,"-");
board.put(6,"-");
board.put(7,"-");
board.put(8,"-");
}
//this is the part I'm trying to work on
StringBuilder result = new StringBuilder();
for ( position = 0; position <= 8; position++) {
if(position % 3 == 0){
result.append("\n");
}
result.append(board.get(position));
}
return result.toString();
Upvotes: 0
Views: 60
Reputation: 826
0 is divisible by 3. In the initial state, your for loop executes the if statement because 0 % 3 == 0
is true
. Change your if to this:
if(position != 0 && position % 3 == 0)
Or you can start position from 1 and handle the 0th input by yourself before the for loop.
Upvotes: 1