Hi, I am working on a program that prompts the user for answers to a quiz. The answers can only be a, b, c or d and they are saved to an array which I later compare to my answer key array. Below is the method I have written to get the user input and validate it but I can't seem to get the validate correct. The way it's written now it always tells me that the input is invalid but then save it and continue on to the next answer prompt. If I enter something other than a, b ,c ,or d it will terminate....where I am I going wrong? Should I be validating at the String for user input or am I validating/comparing the user answers in the wrong manner? Any help is appreciated. Thank you!

     public static char[] getAnswers(char arrayAnswers[ ])
            {
           String userInput; 
           for (int index = 0; index < arrayAnswers.length; index++) 
               {
                userInput= JOptionPane.showInputDialog("Enter the answer to question " + (index +1) + ":");
                arrayAnswers[index] = userInput.charAt(0);
                   while (arrayAnswers[index]!=('a') && (arrayAnswers[index]!=('b')&& arrayAnswers[index]!=('c')&&(arrayAnswers[index]!=('d'))));
                    {
                        JOptionPane.showMessageDialog(null, "Please enter a valid answer");
                        userInput= JOptionPane.showInputDialog("Enter the answer to question " + (index +1) + ":");
                    }

                 }
        return arrayAnswers;
           }

Hello,
When I looked at your code I felt that the "while" loop was trouble :D
The next thing was the use of "&&", which is the logic operator AND therefore you were checking that:
(arrayAnswers[index]!=('a') AND (arrayAnswers[index]!=('b') AND arrayAnswers[index]!=('c') AND (arrayAnswers[index]!=('d'), which to all honesty can never be be true. What you needed was to replace the "||" which represents the logic operator OR. Oh and please please please remove that while loop and make it an if statement.

Here is your working code embeded in my program.

package test;

import javax.swing.JOptionPane;

public class TestUserInput {

    public static char[] getAnswers(char arrayAnswers[ ])
    {
   String userInput; 
   for (int index = 0; index < arrayAnswers.length; index++) 
       {
        userInput= JOptionPane.showInputDialog("Enter the answer to question " + (index +1) + ":");

        if(arrayAnswers[index] != userInput.charAt(0)) {
                JOptionPane.showMessageDialog(null, "Please enter a valid answer");
                userInput= JOptionPane.showInputDialog("Enter the answer to question " + (index +1) + ":");
        }
   }
   return arrayAnswers;
   }

    public static void main(String [] args) {
        char [] arrayAnswers = {'a','b','c','d'};
        TestUserInput.getAnswers(arrayAnswers);
    }
}

Hope this helps.

Thank you so much for replying but I'm still a little confused. In your code you have line 14

     if(arrayAnswers[index] != userInput.charAt(0)) {

what does this mean?

I did change the WHILE to IF and the && to OR/||.However i'm still having the same issue. It says I'm always wrong.

[code]

import javax.swing.JOptionPane;

public class QuizGrader {


   public static void main(String[ ] args)
   {

      // Answer Key array.
      char answerKey[ ] = { 'b', 'd', 'c', 'd', 'c', 'a', 'c', 'b', 'b', 'd' };
      //user's answers array & validate input.
      char [] arrayAnswers = new char[10];
      getAnswers(arrayAnswers);
    }
   public static char[] getAnswers(char arrayAnswers[ ])
   {
       String userInput; 
       for (int index = 0; index < arrayAnswers.length; index++)
       {
        userInput= JOptionPane.showInputDialog("Enter the answer to question " + (index +1) + ":");
        arrayAnswers[index] = userInput.charAt(0);  
            if (arrayAnswers[index]!=('a') || (arrayAnswers[index]!=('b')|| arrayAnswers[index]!=('c')||(arrayAnswers[index]!=('d'))))
            {
              JOptionPane.showMessageDialog(null, "Please enter a valid answer");
              userInput= JOptionPane.showInputDialog("Enter the answer to question " + (index +1) + ":");
             }
        }
        return arrayAnswers;
    }
   }

[/code]

I think your original if test was correct. The problem is the semicolon at the end of line 8, which terminates the while block right there, so the message dialog is not part of the while, and always gets executed. Just get rid of that ;

Awesome it works lol, I just had to add the

arrayUserAnswers[index] = userInput.charAt(0);  

after the reprompt within the while and delete that semi colon in line 8 and it worked. Thank you so much for your help !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.