4
System.out.print("Please select a game: ");
for (String s : gamesArray) {       
    System.out.print(s + ", "); 
}

Output:

Please select a game: spin, tof, Press any key to exit...

The output I am excepting:

Please select a game: spin, tof
Press any key to exit...

Why is it adding another ',' after the last array item? How do I prevent it?

arshajii
  • 127,459
  • 24
  • 238
  • 287
Jony Kale
  • 979
  • 3
  • 15
  • 35
  • Because as coded now, you are printing `,` after each element, including the last element – Steve Kuo Jun 24 '13 at 20:07
  • 7
    It's adding another ", " after the last array item because you told it to add one after every array item. – Louis Wasserman Jun 24 '13 at 20:07
  • You prevent it by writing your code so that the last comma isn't printed. Several different ways to do this, but none are automatic -- you gotta actually think about it and write the code. – Hot Licks Jun 24 '13 at 20:09
  • http://stackoverflow.com/questions/1515437/java-function-for-arrays-like-phps-join has good solutions for this task. – Squeezy Jun 24 '13 at 21:03

7 Answers7

8

Why not just call Arrays#toString(array):

System.out.print("Please select a game: %s%n", 
                  Arrays.toString(gamesArray).replaceAll("(^\\[)|(\\]$)", ""));

OR to avoid regex:

String tmp = Arrays.toString(gamesArray);
System.out.print("Please select a game: %s%n", tmp.substring(1, tmp.length()-1));
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Because that would print: Please select a game: [spin, tof] rather than without the braces. – ardent Jun 24 '13 at 20:11
  • @arshajii: Thanks for your comments. I made it `replaceAll("(^\\[)|(\\]$)", "")` to replace only starting and ending square bracket. – anubhava Jun 24 '13 at 20:17
  • 2
    I'm glad you made the change (+1). But why not just use `substring`? Regular expressions are overkill when all we want to do is strip away the first and last chars. – arshajii Jun 24 '13 at 20:18
  • @arshajii: Unfortunately I have already hit the rep limit for today :( In any case yes substring can also be used but then it just cant be done in one call since we'll need to do `str.substring(1, substring.length()-1);` – anubhava Jun 24 '13 at 20:21
7
// iterate throght array
for ( int i = 0; i < gamesArray.length; i++ ) {

    // get the element
    String s = gamesArray[i];

    // print it
    System.out.print( s );

    // test if the current element is not the last (array size minus 1)
    if ( i != gamesArray.length - 1 ) {

        // if it is not the last element, print a comma and a space.
        System.out.print( ", " );
    }
}
davidbuzatto
  • 9,207
  • 1
  • 43
  • 50
2

Why is it adding another ' , ' after the last array item?

Because there is nothing special about the last item, you are adding , at the end of every element.

How do I prevent it?

You could try using a regular for-loop and only adding , if the index your on is not the last index of the array. Equivalently, you could add , before each element excluding the first, and you will have the same effect.

int len = gamesArray.length;

if (len > 0)
    System.out.println(gamesArray[0]);

for (int i = 1; i < len; i++)
    System.out.print(", " + gamesArray[i]);
arshajii
  • 127,459
  • 24
  • 238
  • 287
2

Can you use the Guava library? If yes, it's one line of code:

String result = Joiner.on(", ").join(gamesArray);
System.out.println(result);
jlordo
  • 37,490
  • 6
  • 58
  • 83
1

The last comma is there because you're printing a comma after each element.

You can fix this by rewriting the for loop as so:

for(int i=0;i<gameArray.length;i++){
    System.out.print(gameArray[i]);
    if(i!=gameArray.length-1){
        System.out.println(", ");
    }
}
William Morrison
  • 10,953
  • 2
  • 31
  • 48
1

Another way to handle this (especially good if you know the array will never be empty) is to write the first element before the loop, then have the loop write , <element>.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0

You have to test if there are more elements in your array before printing a "," to avoid having one after the last game name.

final Iterator<String> iterator = Arrays.asList(gamesArray).iterator();
while(iterator.hasNext()) {
    System.out.print(iterator.next());
    if(iterator.hasNext()) {
        System.out.print(", ");
    }
}
System.out.println();
Raphaël
  • 3,646
  • 27
  • 28