pchova
pchova

Reputation: 99

Validating user Input from Scanner that is added into an Array

I am creating a program that is an exam, and the test Answers are stored in an array and user's Answers are stored in a separate array.

I am trying to write my program so that after each question the user's Answer is loaded into the array, unless they input something other than "A", "B", "C", or "D" . If they do not type one of these values in, then the question will be asked again. I am quite stuck.

Here is my code:

String[] testAnswers = {"B","D","A","A","C","A","B","A","C","D"};
int uT = testAnswers.length; 
String[] userAnswers = new String[uT];

for (int i=0; i<uT;i++) {
System.out.print("Question #"+(i+1)+": ");

userAnswers[i] = in.nextLine();
userAnswers[i] = userAnswers[i].toUpperCase();

}

I have tried to create a Array List and then using an if loop to ask the Q again. The thing is I am not very familiar with ArrayLists.

for (int i=0; i<uT;i++) {
    System.out.print("Question #"+(i+1)+": ");
    userAnswers[i] = in.nextLine();
    userAnswers[i] = userAnswers[i].toUpperCase();


    List<String> list = Arrays.asList(userAnswers);
    while (!list.contains("A") || !list.contains("B") || !list.contains("C") 
    || !list.contains("D")) {
    System.out.println("Invalid Input. ");
    System.out.print("Question #"+(i+1)+": ");
    userAnswers[i] = in.nextLine();
    userAnswers[i] = userAnswers[i].toUpperCase();
    }
}

Another thing I have tried is using a boolean variable, and/or using a do/while loop, but I just can't seem to figure out what to do. I think it's the fact that I put userInput right into an Array that breaks my code.

boolean isValid = true;
do {
    for (int i=0; i<uT;i++) {
        System.out.print("Question #"+(i+1)+": ");
        isValid = false;
        userAnswers[i] = in.nextLine();
        userAnswers[i] = userAnswers[i].toUpperCase();
    try {
    //something here
    }
...

Upvotes: 1

Views: 232

Answers (2)

D. Mayen
D. Mayen

Reputation: 464

I would go with something like this:

for (int i=0; i<uT;i++) {
    System.out.print("Question #"+(i+1)+": ");
    String userCurrentAnswer = in.nextLine();

    while (!userCurrentAnswer.matches("(?i)[A|B|C|D]")) {
        System.out.println("Invalid Input. ");
        System.out.print("Question #"+(i+1)+": ");
        userCurrentAnswer = in.nextLine();
    }
    userAnswers[i] = userCurrentAnswer.ToUpperCase();
}

userCurrentAnswer.matches("(?i)[A|B|C|D]") is for matching any of those characters with case insensitive

Upvotes: 2

Scary Wombat
Scary Wombat

Reputation: 44824

Use a while loop and only increment if input is correct

String[] testAnswers = {"B","D","A","A","C","A","B","A","C","D"};
int uT = testAnswers.length; 
String[] userAnswers = new String[uT];

int i = 0;
while(i<uT) {
    System.out.print("Question #"+(i+1)+": ");

    String input = in.nextLine();
    input = input.toUpperCase();
    if (input.equals ("A") || input.equals ("B") ...) {
        userAnswers[i] = input;
        i++;
    }
    else {
        System.out.println ("Enter again");
    }

}

Upvotes: 2

Related Questions