Answer to Question #282533 in Java | JSP | JSF for Candy

Question #282533

write a Java programming create a arraylist of modulecode in class, use String []{CS401,CS402,CS403,CS404,CS405}


In the main ask user to selected the modulecode in arraylist ,

ask user to choose 3 to 4 modulecode ,

if user choose the same modulecode

,tell user select other one.

if user input error please so the error message.




1
Expert's answer
2021-12-25T01:47:42-0500
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> moduleCodes = new ArrayList<>(Arrays.asList("CS401", "CS402", "CS403", "CS404", "CS405"));
        boolean[] selected = new boolean[moduleCodes.size()];
        Scanner in = new Scanner(System.in);
        int count = 0;
        for (String moduleCode : moduleCodes) {
            System.out.print(moduleCode + " ");
        }
        System.out.println("\nSelect(number) 3 or 4 modulecode:");
        int number = Integer.parseInt(in.nextLine());
        while (count < Math.max(3, Math.min(4, number))) {
            System.out.print(count + 1 + ": ");
            String input = in.nextLine();
            int index = moduleCodes.indexOf(input);
            if (index >= 0) {
                if (!selected[index]) {
                    selected[index] = true;
                    count++;
                    System.out.println("Selected.");
                } else {
                    System.out.println("Not selected because it was previously selected.");
                }
            } else {
                System.out.println("Not exist!");
            }
        }
    }
}

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