Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Trying to validate a character user input in an array ?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to validate a character user input in an array ?

    Hi all, 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 loop correct. The way it's written now it will always tell 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 < arrayUserAnswers.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 letter!");
    				   				userInput= JOptionPane.showInputDialog("Enter the answer to question " + (index +1) + ":");
    				   			}
     
    		}
    		return arrayAnswers;
    	   }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Trying to validate a character user input in an array ?

    There is an array, arrayUserAnswers[], that is only used in the method to determine how many answers to gather. Don't you mean to store the user's answers in that array? The comparison you're currently doing to determine if a proper response was given is to the answer key array. Maybe that should work too, but we don't know what's in either array at this point.

    BTW, The retry portion of your code will not provide an updated answer, because the new userInput value is not used.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to validate a character user input in an array ?

    oops sorry I missed that on my part, I've changed it to the correct arrayAnswers[] since my user answers are saved to the arrayAnswers[] (my key is actually name answerKey[i] and it's not used in this method).

    Did you mean that my retry portion wouldn't return an updated answer because I was missing "arrayAnswers[index] = userInput.charAt(0);" within that loop? I've changed it but the program now prompts for an answer, regardless of the answer it will tell me it's invalid, then it prompts for answer again but it's the wrong answer #

    example
    1. prompt answer #1
    2. input is 'a'
    3. user input = invalid, <=== this wrong should validate and move to next prompt
    4. prompts user again for input but this time it's for quiz answer 2 skipping gathering the correct answer 1 altogether <===this is wrong as well

    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 letter!");
    			     userInput= JOptionPane.showInputDialog("Enter the answer to question " + (index +1) + ":");
                                 arrayAnswers[index] = userInput.charAt(0);
    		       }
    		}
    		return arrayAnswers;
    	   }

Similar Threads

  1. How to validate user input in a loop
    By camel-man in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 8th, 2013, 01:01 PM
  2. How to add to an array with user input
    By miller4103 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2013, 10:27 PM
  3. How do I use try-catch to validate input?
    By BiaxialPainter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 15th, 2012, 10:59 AM
  4. Placing user input to a array and to then display it as a string
    By LaliB in forum Collections and Generics
    Replies: 5
    Last Post: January 12th, 2012, 11:41 AM
  5. Append data from user input into array
    By brncao in forum Collections and Generics
    Replies: 1
    Last Post: October 11th, 2011, 04:37 AM

Tags for this Thread