-1

i have a little missbehaviour in my program.

One of his methods should build an string and then return it to be displayed in an Java Swing GUI. In some parts of it, it needs to store a "line break" (i dont know the proper name) "/n".

But when i display this string in the gui it contains /n instead of jumping the line properly.

Its a little confusing but with the code i think you can figure it out:

public String draw(Robot robot, MapClass mapa) {
        map = mapa.getMapa();
        String mapBuffer = "";
        for (int i = 0; i < mapa.getLinhas(); i++) {
            for (int j = 0; j < mapa.getColunas(); j++) {
                if ((i == robot.getPosX()) & (j == robot.getPosY())){
                    System.out.print(robot.convertToChar() + " ");
                    mapBuffer = mapBuffer + robot.convertToChar() + " ";
                }
                else{
                    System.out.print(map[i][j] + " ");
                    mapBuffer = mapBuffer + map[i][j] + " ";
                }   
            }
            mapBuffer = mapBuffer + "/n";
            System.out.println();
        }

        return mapBuffer;
}

This string is shown in a swing textArea with:

textArea.append(drawer.draw(robot, map));

Thanks.

Diedre
  • 515
  • 4
  • 21
  • 9
    Line break is `'\n'`, not `'/n'`. – Sergey Kalinichenko Sep 23 '14 at 21:19
  • lol such an stupid error. Thanks -.- – Diedre Sep 23 '14 at 21:20
  • 2
    Also - for efficiency, you should consider using a StringBuilder to build up your return string instead. – Michael Krause Sep 23 '14 at 21:23
  • 2
    And for platform-independency trade you may want to replace `\n` by `%n` and return `String.format(mapBuffer)`. See [How do I get a platform-independent new line character?](http://stackoverflow.com/questions/207947/java-how-do-i-get-a-platform-independent-new-line-character) – dic19 Sep 23 '14 at 21:28

1 Answers1

1

The problem is here mapBuffer = mapBuffer + "/n"; You are appending to mapBuffer the /n literal. If you want a line break you should replace it with \n

gkrls
  • 2,618
  • 2
  • 15
  • 29
  • As stated before, thanks all for awnsering such an stupid error without judging, i cant belive i didnt see that after a lot of time thinking it was a problem with swing... – Diedre Sep 25 '14 at 23:58