Results 1 to 8 of 8
  1. #1
    vividcooper is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Exclamation urgent urgent pls String matching a pattern

    I'm writing a code where I need to match the input by the user to a String Pattern. I need to ensure that the user enters the right pattern matching the one I defined. if the user enters something different from the pattern the program must asks user to enter the right data again...
    this is my code but even if the user enters the right data (which is Id number) the program is telling that is is invalid and it only loops twice...

    final String CODE = "[0-9]{6} + [a-zA-Z]{1}";

    /*Creating String specification so as to validate the data entered by the user.
    The String should follow the pattern as specified. It should contain six numbers from zero to nine
    with the last character being a letter either in lower or in upper case.
    */



    String id;
    boolean match;

    System.out.println("Enter client's ID:\n ");
    id = sc.next();


    do{


    if(id.matches(CODE)){
    match = true;
    break;}

    else {
    match = false;
    System.out.println("ID NOT VALID !!");
    System.out.println("Enter client's ID again :\n ");
    id = sc.next();
    }
    }while(id.matches(CODE));


    can someone help me please? I really need help thanks
    Last edited by vividcooper; 01-06-2010 at 10:41 PM. Reason: need reponse urgently

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    That "+" in the regex doesn't look right. You don't mention a plus sign being part of the id number.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Your urgency is your problem. I say that not to be unkind or uncaring, but it's just a fact. Most people react negatively at any attempt to "fast track" a problem. After all the other problems posted here, aren't they (or might they not be) urgent?

    Implicitly you are denigrating other people's requests for help. I'm sure you don't mean to. A better way of getting help most quickly would be to post actual compilable code and describe the problem in a way that is as objective as possible. For instance you might give examples of input and describe the intended program output as well as the output you actually observe.

  4. #4
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Look at your loop. It gets a String from the user, then checks if it matches (it doesnt), so the if statment returns false and the user is prompted again. Again, it doesn't match so the do-while loop ends, thats why it only runs twice.

  5. #5
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    "[0-9]{6} + [a-zA-Z]{1}"; matches any 6 digits followed by a minimum of TWO spaces and then one letter.

    MATCHES:
    Java Code:
    123456                      h
    555555  A
    093821     z
    ^ has to be at least 2 spaces between the 6 digits and the letter.

    DOES NOT MATCH THIS:
    Java Code:
    123456h
    51235251232 A
    Please give us an example of a valid ID.

  6. #6
    vividcooper is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default

    hi sorry if I reacted a little bit crazy but I'd been making changes for this loop so long xD
    I apologize...

    the id must be in the format like the next example:
    123456A

    thanks once again

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by vividcooper View Post
    I'm writing a code where I need to match the input by the user to a String Pattern. I need to ensure that the user enters the right pattern matching the one I defined. if the user enters something different from the pattern the program must asks user to enter the right data again...
    this is my code but even if the user enters the right data (which is Id number) the program is telling that is is invalid and it only loops twice...

    final String CODE = "[0-9]{6} + [a-zA-Z]{1}";

    /*Creating String specification so as to validate the data entered by the user.
    The String should follow the pattern as specified. It should contain six numbers from zero to nine
    with the last character being a letter either in lower or in upper case.
    */



    String id;
    boolean match;

    System.out.println("Enter client's ID:\n ");
    id = sc.next();


    do{


    if(id.matches(CODE)){
    match = true;
    break;}

    else {
    match = false;
    System.out.println("ID NOT VALID !!");
    System.out.println("Enter client's ID again :\n ");
    id = sc.next();
    }
    }while(id.matches(CODE));


    can someone help me please? I really need help thanks
    About your control flow structure: you don't want to continue looping if the code is correct so change your while condition to:
    Java Code:
    }
    while (true);
    ... you are doing the actual match in the body of your loop and break out of it if the code matches which is correct.

    If you want six digits followed by one or more spaces followed by a single letter make your pattern:
    Java Code:
    "[0-9]{6}\s+[a-zA-Z]"
    ... the \s pattern matches a single white space character.

    kind regards,

    Jos

  8. #8
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    This pattern will match any 6 digits followed by a letter:

    Java Code:
    "\\d{6}[a-zA-Z]"
    ^notice that you need \\ in java because \ is a special character. to make \ a special character in regex, you need \\ in java.

Similar Threads

  1. Replies: 1
    Last Post: 10-27-2009, 07:33 PM
  2. regular expressions and string matching
    By DennyLoi in forum New To Java
    Replies: 1
    Last Post: 11-16-2007, 10:15 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •