0

So whenever I try to append a new line using a StringBuilder, I can't get a new line whatsoever, I tried:

errorMessage.append(System.getProperty("line.separator"));
errorMessage.append(System.getProperty("\n"));
errorMessage.append(System.getProperty("\r\n"));
errorMessage.append(System.getProperty("line.separator"));

basically everything within the first 3 pages of google results, it's so frustrating. I am implementing it in a for loop like this : idk if it helps, but any suggestions are appreciated.

public String getIDs(HashMap<String,List<Integer>> errorMap ){
    StringBuilder errorMessage = new StringBuilder();

    for (String state:errorMap.keySet()){

        List<Integer> listofId = errorMap.get(state);

        if (listofId){
            StringBuilder listOfIds = new StringBuilder();
            for (Integer id :listofId) {
                listOfIds.append(id.toString()+' , ')
            }
            errorMessage.append(state +" Trades: " +listOfIds.toString())
            errorMessage.append("\n")
        }
    }
    return errorMessage.toString();
}
James Kleeh
  • 12,094
  • 5
  • 34
  • 61
UWGOOSE
  • 833
  • 3
  • 11
  • 20
  • 3
    There is no system property called `\n`. Just use `errorMessage.append("\n")`. – Andy Turner Dec 11 '17 at 22:59
  • 2
    Also: you've got lots of missing `;`s, and `' , '` should be `" , "`. Unless this is gradle, or some such, in which case please tag it. – Andy Turner Dec 11 '17 at 23:00
  • You can't possibly have tried `errorrMessage.append(System.getProperty"line.separator"));`, because it would have worked. Unclear what you're asking, unless it is merely how to fix your syntax errors. – user207421 Dec 11 '17 at 23:48
  • @EJP That's actually the first thing I tried but it didn't work, the whole text still came out as one single line.... I do think that I miss tagged tho, I am actually working on a grails project so could it be an invalid groovy syntax? – UWGOOSE Dec 12 '17 at 02:10
  • @UWGOOSE Does your code do any loading of any system properties? Post that code. I'm suspecting that something is wiping out all your system properties. – Andrew Henle Dec 12 '17 at 10:51

2 Answers2

0

Use

errorMessage.append("\n");

Instead of

errorMessage.append(System.getProperty("\n"));
fralewsmi
  • 92
  • 2
  • 12
  • `System.getProperty("line.separator")` [should work](https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html). – Andrew Henle Dec 11 '17 at 23:02
  • System.lineSeparator() for java7 and later. Also see https://stackoverflow.com/questions/207947/how-do-i-get-a-platform-dependent-new-line-character – geco17 Dec 11 '17 at 23:04
  • Actually yeah, I got confused about what the poster was even asking – fralewsmi Dec 11 '17 at 23:04
  • @AndrewHenle I tried that method didn't work tho, neither did System.lineSeparator(), do you know any reason why it wouldn't work? – UWGOOSE Dec 12 '17 at 02:07
  • @UWGOOSE I have no idea why that wouldn't work. It's pretty basic. The only thing I can think of is something completely wiped your system properties. Can you post code demonstrating it not working? If so, that should probably be a separate question at this point. – Andrew Henle Dec 12 '17 at 10:50
0

You should directly be using builder.append("\n"). \n is not a property.

Also append method returns builder object itself (Builder pattern). So you can easily do builder.append("\n").append("text1").append("\n").append("text2").....

Dev Amitabh
  • 185
  • 1
  • 4