Add comma/and between string array in java i get an array of authors from json response and i have to display authors name in TextView(android) in proper format like =&gt;Viraj, Chetan and George R. R. Martin
my code work fine, but it's a mess....
public class SeprateAuthors {
public static void main(String[] args) {
    String[] authors0 = {&quot;a&quot;, &quot;b&quot;};
    String[] authors1 = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;};
    String[] authors2 = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;,&quot;d&quot;};
    String[] authors3 = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;};
    
    System.out.println(displayAuthors(authors0));
    System.out.println(displayAuthors(authors1));
    System.out.println(displayAuthors(authors2));
    System.out.println(displayAuthors(authors3));        
}
public static String displayAuthors(String[] authors) {
    StringBuilder stringBuilder = new StringBuilder();
    String stringAuthors=&quot;&quot;;
    String prefixComma = &quot;, &quot;;
    String prefixAnd = &quot; and &quot;;
    if ((authors != null) &amp;&amp; (authors.length &gt; 0)) {
        
        for (int i = 0; i &lt; authors.length; i++) {
            if (i &lt; authors.length - 2) {
                stringBuilder.append(authors[i]).append(prefixComma);
            } else {
                stringBuilder.append(authors[i]).append(prefixAnd);
            }
        }
        //  Java Remove extra Characters(&quot;and &quot;) from String
        stringAuthors = stringBuilder.substring(0, stringBuilder.length() - 4);
    }
    return stringAuthors;
}

}
Welcome to Code Review, I have some hints about your code, starting from your main method:
public static void main(String[] args) {
    String[] authors0 = {&quot;a&quot;, &quot;b&quot;};
    String[] authors1 = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;};
    String[] authors2 = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;,&quot;d&quot;};
    String[] authors3 = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;};
    
    System.out.println(displayAuthors(authors0));
    System.out.println(displayAuthors(authors1));
    System.out.println(displayAuthors(authors2));
    System.out.println(displayAuthors(authors3));        
}

You have defined several arrays forcing the multiple print , you can obtain the same result defining an 2d array storing arrays of different dimensions like below :
public static void main(String[] args) {
    String[][] authors = { {&quot;a&quot;},
                           {&quot;a&quot;, &quot;b&quot;},
                           {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;},
                           {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;,&quot;d&quot;},
                           {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;}};
    
    for (String[] row : authors) {
        System.out.println(displayAuthors(row));
    } 
}

About the print of the authors array you can distinguish three cases :

array with zero elements : you will print the &quot;&quot; empty string.
array with one element : you will print the unique element of the
array.
array with more than one element : you will print elements with &quot;, &quot;
as separator except for the penultimate element and the last one
that will be separated by &quot;and&quot;.

You can rewrite your displayAuthors method in this way:
public static String displayAuthors(String[] authors) {
    if (authors == null) { return &quot;&quot;; }
        
    switch (authors.length) {
        case 0: return &quot;&quot;;
        case 1: return authors[0];
        default: return helperDisplayAuthors(authors);
    }
}

I defined a new method called helperDisplayAuthors :
private static String helperDisplayAuthors(String[] authors) {
    final int length = authors.length;
    String result = &quot;&quot;;
        
    for (int i = 0; i &lt; length - 2; ++i) {
        result += authors[i] + &quot;, &quot;;
    }
        
    return String.format(&quot;%s%s and %s&quot;, result, 
            authors[length - 2], authors[length - 1]);
    }

}


The method String.format helps you to compose the final string. Here the code of your class:
public class SeparateAuthors {

    public static void main(String[] args) {

        String[][] authors = {{&quot;a&quot;},
                              {&quot;a&quot;, &quot;b&quot;},
                              {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;},
                              {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;},
                              {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;, &quot;f&quot;}};

        for (String[] row : authors) {
            System.out.println(displayAuthors(row));
        }
    }
    
    public static String displayAuthors(String[] authors) {
        if (authors == null) { return &quot;&quot;; }
        
        switch (authors.length) {
            case 0: return &quot;&quot;;
            case 1: return authors[0];
            default: return helperDisplayAuthors(authors);
        }
    }

    private static String helperDisplayAuthors(String[] authors) {
        final int length = authors.length;
        String result = &quot;&quot;;
        
        for (int i = 0; i &lt; length - 2; ++i) {
            result += authors[i] + &quot;, &quot;;
        }
        
        return String.format(&quot;%s%s and %s&quot;, result, 
                authors[length - 2], authors[length - 1]);       
    }

}