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?
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With