0

I have the following java code:

String strTest = null;

for (AlternativeEntity alternativeEntity : msg.Guidance()
      .getAlternatives()) {

    strTest = strTest + alternativeEntity.getArrivalStation().getName() + ", ";

}

The output looks like this:

nullabc, xyz, oop, 

How can I solve this problem and very bad character format? It would be great if I can create output like this:

abc, xyz, oop
2
  • 3
    You could use a library like Guava. See stackoverflow.com/questions/1751844/… Commented Oct 10, 2013 at 11:51
  • You should use StringBuilder for this nor String. Commented Oct 10, 2013 at 11:59

9 Answers 9

3

Initialize your string to "":

 String strTest = "";

Alternatively, you should use a StringBuilder:

 StringBuilder builder = new StringBuilder();

 for (AlternativeEntity alternativeEntity : msg.Guidance()
  .getAlternatives()) {

     builder.append(alternativeEntity.getArrivalStation().getName()).append(", ");

 }

This will produce better performance.

Sign up to request clarification or add additional context in comments.

3 Comments

Why the boilerplate? Why don't you use Guava for example.
Also remove the last , after the loop. Your answer is the best, but nobody paid attention to that detail.
Maybe also include about removing the trailing ", ".
3

Initialize strTest as:

String strTest = "";

Also, remove the last comma ,

strTest=strTest.substring(0, strTest.length()-1);

1 Comment

Or use Guava, which can do all the stuff for you in a single line. :)
3

You can use Guava's Joiner#join(Iterable parts). For example:

Joiner joiner = Joiner.on(", ").skipNulls();
String result = joiner.join(list);
System.out.println(result);

Here, all the elements of the list will be printed comma separated without any trailing commas. Also, all the null elements will be skipped.

More info:

3 Comments

That's what I wanted to answer. +1
having a separate library just to join the lines together seems like an overkill. Also, if we're going to use libraries, is there some reason to use Guava instead of commons-lang? StringUtils.join(list, ", "); seems simpler than this example.
I haven't said Guava is the best. It's just an option. Also, I would say using Guava is an overkill - it's just an utility library. :)
1

Java provides StringBuilder class just for this purpose,its simple and easy to use..

    StringBuilder str = new StringBuilder("India ");

     //to append "Hi"
    str.append("Hi");

    // print the whole string
    System.out.println("The string is "+str)

the output will be : The string is India Hi

click here to know more about StringBuilder class

Comments

0

Replace String strTest = null; by String strTest = "";

Comments

0

Change

 String strTest = null;

to

String strTest = "";

Comments

0

why don't you use:

String strTest = "";

and at the end:

if(strTest.endsWith(", "))
   strTest = strTest.substring(0, strTest.length()-2);

Comments

0

Initialize String strTest="";

For skipping the last comma',' Use outside For loop:

strTest = strTest.substring(0,strTest.trim().length()-1);

Comments

0
String strTest = null;

for (AlternativeEntity alternativeEntity : msg.Guidance().getAlternatives()) {
  String name = alternativeEntity.getArrivalStation().getName();
  strTest = (strTest == null) ? name : strTest +  ", " + name;
}

If the list is long, you should use a StringBuilder rather than the String for strTest because the code above builds a fresh string on each iteration: far too much copying.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.