Answer to Question #323294 in Java | JSP | JSF for Gerald

Question #323294

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


1
Expert's answer
2022-04-04T16:12:07-0400
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);

    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS