-1

I created this little method to make taking a list return a string with all the elements. But I wanted to add the fact that it returned me in column, for example:

Hello
to
All

I tried to insert the "\ n" but it doesn't change the result.

    public static String fromListToString(List list) {
    String string= "";
    for (int i = 0; i < list.size(); i++) {
        string+= "\n" + list.get(i);
    }
    return string;
}

How can I do?

Jacket
  • 353
  • 6
  • 18
  • How did you run this? How do you print the output? `\n` sould work in most systems. – Hulk Apr 22 '22 at 08:13
  • While you're at it, why not simply [join](https://stackoverflow.com/q/1751844/205233) your strings with the newline char? As for your problem at hand: I'd try including the carriage return like so `"\r\n"`. – Filburt Apr 22 '22 at 08:17
  • Hi @Filburt I don't understand, could you give me an example related to my code? – Jacket Apr 22 '22 at 08:23
  • @Jacket Have you tried the solution in the post I linked? – Filburt Apr 22 '22 at 08:24
  • 2
    BTW that function can (almost **¹**) be simplified to `String.join("\n", list)`, or using the system line separator: `String.join(System.lineSeparator(), list)` || **¹** missing the leading new line – user16320675 Apr 22 '22 at 08:54
  • Also note: it seems you are using **raw types** here, as you don't specify that you have a `List` here. And `list` is a terrible name for a variable. Rather say what you EXPECT to be in that list, like `elementNames` or something like that. The IDE can tell you the type of the variable, there is no point in hard-coding the TYPE in the name! – GhostCat Apr 22 '22 at 08:54
  • @GhostCat You're right, thanks for the advice! – Jacket Apr 22 '22 at 09:04
  • Please tell us: How did you run this? How do you print the output? On which system? And what is the exact output you are seeing? – Hulk Apr 22 '22 at 09:04
  • @Hulk I am starting it in AEM, for the title component, to have a title in column and not for horizontal, it is a simple GetTitle method that is invoked – Jacket Apr 22 '22 at 09:07
  • 1
    @Hulk The problem is definitely AEM, thanks for the replies – Jacket Apr 22 '22 at 09:08

2 Answers2

2

This code works on my system (eclipse on windows), but to make it platform independent you can use System.lineSeparator():

public static void main(String[] args) {
    List<String> list = List.of("Hello", "to", "all");
    System.out.println(fromListToString(list));
}

public static String fromListToString(List<?> list) {
    String string= "";
    for (int i = 0; i < list.size(); i++) {
        string +=  System.lineSeparator() + list.get(i);
    }
    return string;
}

One thing I also fixed: note the List<?> instead of the rawtype List. This does not cause any problems here, but it might later on if you reuse this somewhere.

Ouput:


Hello
to
all

(there is a leading blank line because of the first \n).

If you are still seeing the output in a single line, this is probably due to the way you print the result. Some loggers, for example, remove all linebreaks from messages. If you are writing the result to a file, you may need to make sure the correct line separator for your system is used, see How do I get a platform-dependent new line character?

Hulk
  • 6,399
  • 1
  • 30
  • 52
  • The result instead of the string is a copy error, I have updated the code! Sorry for my mistake – Jacket Apr 22 '22 at 08:31
  • When you know that hardcoding the newline is bad, why are putting up example code that does it the bad way? – GhostCat Apr 22 '22 at 08:47
  • Yeah well, that's the point: maybe don't invest a lot of time in answering unclear questions. Rather tell the OP to look out for [mcve] and improve the question. *Guessing* what might be going on isn't the ideal option. – GhostCat Apr 22 '22 at 08:53
  • @GhostCat agreed. I sometimes still fall into that trap. – Hulk Apr 22 '22 at 09:02
  • 1
    I thought it was an error in my code, instead it is due to some configuration of Aem, thanks to everyone for the answer – Jacket Apr 22 '22 at 09:08
0
public static String fromListToString(List list) {
    String result = "";
    for (int i = 0; i < list.size(); i++) {
        result += "\n" + list.get(i);
    }
    return result;
}

Matter of typo. :) Your String was called string, and not result.

New answer:

Use:

System.getProperty("line.separator")

Instead of:

\n

The linebreak character is not "\n" on every operating system.

Filburt
  • 17,626
  • 12
  • 64
  • 115
David Weber
  • 173
  • 9