Question:
How to print out elements from an array with a comma between elements except the last word?
Code:
public class PrintWords { public static void main(String[] args) { String[] words = new String[] {"apple","pear","grape","orange"}; System.out.print(words[0]); for(int i=1;i<words.length;i++){ System.out.print("," + words[i]); } System.out.println(); } }
Output:
$ java PrintWords apple,pear,grape,orange