Have you ever answered a test question that tells you to enumerate a series of things, but the answer must be in a sentence? Quite tiring to write those things separated by commas and a single space, isn't it?
Then let's try coding it instead
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
List<String> arr = new ArrayList<>();
String answ = "";
System.out.print("Enter number of items: ");
int qua = in.nextInt();
for(int i = 0; i < qua; i++){
System.out.print("item " + (i+1) + ": ");
arr.add(in.next());
}
for(int i = 0; i < arr.size(); i++){
if(i == arr.size()-1){
answ += arr.get(i) + ".";
continue;
}
answ += arr.get(i) + ", ";
}
System.out.println(answ);
}
}
Comments
Leave a comment