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: Single Character Validation

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Single Character Validation

    Hello,
    I am having a bit of trouble validating single Chars inputted by the user for my intro to java class. I usually validate it with a while loop when its numbers (double, int, etc) but i cant seem to find a right way to validate these.


    the program is to ask the user to answer to a test of 20 questions ( A,B,C or D) and then store it in an array.This is what i have so far (minus the rest of the code). no syntax errors given by netbeans =/


    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter Your Answers for the Exam "
    + "(CAPS LOCK ON!)");

    for (int index = 0; index < 19; index++)
    {
    System.out.println("Enter Answer for Question #" + (index + 1)
    + ":");

    givenAnswers[index] = keyboard.next().charAt(0);

    while (givenAnswers[index] != 'A' || givenAnswers[index] != 'B' ||
    givenAnswers[index] != 'B'|| givenAnswers[index] != 'C')
    {
    System.out.println("Please Enter a Valid Answer (A,B,C, or D)");
    givenAnswers[index] = keyboard.next().charAt(0);
    }
    }

    this is what i get in the run window(its like its not validating at all??? and just keeps going) :

    run:
    Enter Your Answers for the Exam (CAPS LOCK ON!)
    Enter Answer for Question #1:
    A
    Please Enter a Valid Answer (A,B,C, or D)
    B
    Please Enter a Valid Answer (A,B,C, or D)
    C
    Please Enter a Valid Answer (A,B,C, or D)
    D
    Please Enter a Valid Answer (A,B,C, or D)

    any thoughts?? thanks in advanced


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Single Character Validation

    while (givenAnswers[index] != 'A' || givenAnswers[index] != 'B' ||
    givenAnswers[index] != 'B'|| givenAnswers[index] != 'C')

    It would seem that what you're saying is, with the or, that the loop should continue while givenAnswers[index] is not equal to ALL of those. Try it with && instead of ||.

    Also, you seem to have B in there twice.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Single Character Validation

    IT WORKEDDD!! THANK YOU!! and yeah i noticed the two b's there too. I changed them now.

Similar Threads

  1. JSF Validation with JSR-303
    By Mr.777 in forum Web Frameworks
    Replies: 0
    Last Post: January 5th, 2012, 08:33 AM
  2. Replies: 3
    Last Post: April 11th, 2011, 09:51 PM
  3. Help With Validation
    By bengregg in forum Loops & Control Statements
    Replies: 4
    Last Post: February 1st, 2011, 04:24 AM
  4. Exception during xml validation
    By vijeta in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 6th, 2010, 02:50 AM
  5. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 12:13 PM