Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

newline character indenting? (Java)

I am trying to use \n to create a list of strings but whenever I add more than one string, the string indents up to the point of the previous string so an example output would be:

0 hello nain
            1 test nain
                       2 huh nain

I don't know why its doing this. Here's the code where I create the string:

            String[] posts = currentTopic.getMessages("main");
            //String[] postsOutput = new String[posts.length];
            String postsOutput = "0 " + posts[0];

            for(int i = 1; i < posts.length; i++){
                postsOutput += "\n" + i + " " + posts[i];
                //postsOutput[i] = i + posts[i];
            }

            sc.close();
            return postsOutput;

I also tried moving the \n to the end of the appending but the result is still the same. Any help would be appreciated.

Thanks

like image 797
nain33 Avatar asked Jun 02 '26 12:06

nain33


1 Answers

that looks like you're on a system where "\n" just gets down (line feed) and you're missing the required carriage return.

That shouldn't be something you should care yourself: line.separator property is adjusting to the host operating system so that it behaves like System.out.println.

like image 168
PypeBros Avatar answered Jun 04 '26 02:06

PypeBros



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!