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
David Makogon
  • 69,407
  • 21
  • 141
  • 189

9 Answers9

3

Initialize strTest as:

String strTest = "";

Also, remove the last comma ,

strTest=strTest.substring(0, strTest.length()-1);
Masudul
  • 21,823
  • 5
  • 43
  • 58
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:

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • That's what I wanted to answer. +1 – Adam Arold Oct 10 '13 at 11:54
  • 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](http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#join%28java.lang.Iterable,%20java.lang.String%29)? `StringUtils.join(list, ", ");` seems simpler than this example. – eis Oct 22 '13 at 13:30
  • 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. :) – Konstantin Yovkov Oct 22 '13 at 13:31
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.

Evan Knowles
  • 7,426
  • 2
  • 37
  • 71
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

Amith
  • 6,818
  • 6
  • 34
  • 45
0

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

Julien
  • 2,544
  • 1
  • 20
  • 25
0

Change

 String strTest = null;

to

String strTest = "";
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

why don't you use:

String strTest = "";

and at the end:

if(strTest.endsWith(", "))
   strTest = strTest.substring(0, strTest.length()-2);
vishal_aim
  • 7,636
  • 1
  • 20
  • 23
0

Initialize String strTest="";

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

strTest = strTest.substring(0,strTest.trim().length()-1);
Pranav Kale
  • 629
  • 1
  • 5
  • 13
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.

Gene
  • 46,253
  • 4
  • 58
  • 96