Quite tiring to write those things separated by commas and a single space, isn't it?
Then let's try coding it instead
Five lines containing a string on each.
A single line containing all the strings separated by a comma and a space.
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String[] arr = new String[5];
String answ = "";
for(int i = 0; i < arr.length; i++) {
System.out.print("Enter line " + (i+1) + ": ");
arr[i] = in.nextLine();
}
for(int i = 0; i < arr.length; i++){
if(i == arr.length-1){
answ += arr[i] + ".";
continue;
}
answ += arr[i] + ", ";
}
System.out.println(answ);
}
}
Comments
Leave a comment