Aurora_Titanium
Aurora_Titanium

Reputation: 472

How not to display commas at the start and the end of the list

I have a toString() method. How do I get it to print [A,B,C,D] instead of [,A,B,C,D,]

  public String toString()
  {
    String result = "[";

    if (numItems > 0)
    {
      for (int i = 0; i < queueArray.length && queueArray[i] != null; i++)
      {
          result+= queueArray[i].toString() + ",";
      }    
      result += "]";
    }
    else
    {
      result = "[ ]";
    } 
    return result;
  }

Upvotes: 0

Views: 143

Answers (7)

Viet
Viet

Reputation: 3409

use one line with String.join

String result = String.format("[%s]", String.join(",", queueArray));

Upvotes: 0

shmosel
shmosel

Reputation: 50716

public String toString() {
    return Arrays.stream(queueArray)
            .filter(Objects::nonNull)
            .collect(Collectors.joining(",", "[", "]"));
}

Upvotes: 2

Marco Z.
Marco Z.

Reputation: 61

for (int i = 0; i < queueArray.length && queueArray[i] != null; i++)
{
      result+= queueArray[i].toString() + ",";
}
result = result.Substring(0,result.length() - 1);

Just add the above code line after the for loop and you are good to go

Upvotes: 0

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17132

Use a StringJoiner:

public String toString() {
    StringJoiner sj = new StringJoiner(","); // Use commas to attach

    if (numItems > 0) {
        for (int i = 0; i < queueArray.length && queueArray[i] != null; i++) {
            sj.add(queueArray[i].toString()); // Loop through & attach
        }
        sj.add("]");
    } else {
        sj.add("[ ]");
    }

    return sj.toString();
}

Here is another sample program, so as to clarify how it works:

public static void main(String[] args) {
    // You pass in the "joiner" string into the StringJoiner's constructor 
    StringJoiner sj = new StringJoiner("/"); // in this case, use slashes 
    // The strings to be attached
    String[] strings = new String[]{"Hello", "World", "!"};

    for (String str : strings)
        sj.add(str); // Attach

    System.out.println(sj.toString()); // Print the content of the StringJoiner
}

And the output is:

Hello/World/! // No slash in the end

Upvotes: 3

SatyaTNV
SatyaTNV

Reputation: 4135

    //check whether its last item or not, to skip the appending comma at the end
    if(i==queueArray.length-1)
      result+= queueArray[i].toString();
    else
      result+= queueArray[i].toString() + ",";

OR

Simply,

Arrays.toString(queueArray)

Upvotes: 1

Calvin P.
Calvin P.

Reputation: 1232

public String toString() {
    String result = "[";
    if (numItems > 0) {
        for (int i = 0; i < queueArray.length && queueArray[i] != null; i++) {
            if(i!=0) result += ",";
            result+= queueArray[i].toString();
        }
        result += "]";
    }
    else {
        result = "[ ]";
    }
    return result;
}

Upvotes: 0

Zamrony P. Juhara
Zamrony P. Juhara

Reputation: 5262

You should iterate from i=0 to array length-2 to concatenate string with coma. At last element than you do neet to concat it with coma.

for(int i=0; i<queueArray.length; i++) {
    result += queueArray[i].toString;
    if (i != queueArray.length-1) {
       result += ",";
    }
}

Upvotes: 0

Related Questions