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 5 of 5

Thread: Input Validation - Take no Symbols or Numbers

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Input Validation - Take no Symbols or Numbers

    So what I'm trying to do is take String input and validation to the following correspondences:

    1. Take no Digits "1234567890"
    2. Take no Symbols/Special Characters "!@£$%^&*()"

    OUTPUT FROM TESTING FOLLOWING CODE:

    Please enter your name:
    John Smith2323
    Found a special character
    Name: J Smith2323

    So... The program has detected a special character, or a digit. So it printed "Found Special Character..."... So my code is half way there...

    What I need to do, is detect the special character, and if this is detected then it should ask for the input again. It will keep asking for the input until the input has only a letter values and a whitespace.

    I might need a loop in here somewhere, but I am not sure where I should put it... or how to structure it... maybe some boolean values as well?

    System.out.println("Please enter your name: ");
                n = kbd.nextLine(); 
               char[] chars = n.toCharArray();
               boolean sChar = false;
               for(char c : chars) {
                   if(!Character.isLetter(c) && (!Character.isWhitespace(c))) {
                       sChar = true;
                       }
                   }
                   if(sChar == true) {
                       System.out.println("Found a special character");
                   }


  2. #2
    Member
    Join Date
    Nov 2013
    Location
    Bangalore, India
    Posts
    70
    My Mood
    Cool
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Input Validation - Take no Symbols or Numbers

    Do it on a while loop until your validation is successful you must keep taking input.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Input Validation - Take no Symbols or Numbers

    Not very helpful. I already know I need a while loop. But, I just don't know where it should be, or how it should be structured≈...

  4. #4
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Input Validation - Take no Symbols or Numbers

    Why don't you use Java Regular Expression ?

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Input Validation - Take no Symbols or Numbers

    I think a Do-While loop will be a good start!
    Since your program is going to have to test the name every time it is entered it ought to be in the loop body, and the do-while allows for a post-loop-coninuation-testing. Maybe you can figure out what the loop continuation condition will be.

    do
    {loop body}
    while (loop-continuation-condition)

Similar Threads

  1. How to use a while loop to perform input validation?
    By namenamename in forum Java Theory & Questions
    Replies: 11
    Last Post: October 5th, 2013, 07:24 AM
  2. Replies: 1
    Last Post: August 9th, 2013, 10:26 AM
  3. Replies: 4
    Last Post: February 10th, 2013, 11:58 PM
  4. HELP! Input, numbers, nonsense
    By Corvus in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2010, 03:18 PM
  5. Input Validation
    By nic in forum AWT / Java Swing
    Replies: 4
    Last Post: November 18th, 2009, 10:54 AM