1

I have a text and I need to validate it against the following rules:

  1. The text must have a length of 8 characters
  2. The text must have a leading letter
  3. The text must have a trailing letter
  4. The text must have 6 numbers between the 2 letters

A good example is:L123456X or l123456x

I did it the following way:

public boolean isValidated(String str) {
    boolean validated = false;

    char a = str.charAt(0);
    char z = str.charAt(str.length() - 1);
    int i;
    if (str.length() == 8 && Character.isLetter(a) && Character.isLetter(z)) {
        validated = true;
        for ( i = 1; i < str.length() - 1; ++i ) {
            if(Character.isLetter(str.charAt(i))) {
                validated = false;
                break;
            }
        }
    }
    return validated;
}

Is there a simpler way?

  • Where are you checking your first rule: that the string has a length of `8`? – Robert Sep 26 '14 at 14:52
  • Try looking into regular expressions. You should be able to match a single letter, at least six alpahnumeric characters, and one more letter with one regex pattern. Something like `^[a-zA-Z][a-zA-Z0-9]{6,}[a-zA-Z]$` - then use a regex matching method to test your string. – Surreal Dreams Sep 26 '14 at 14:52
  • possible duplicate of [Validating input using java.util.Scanner](http://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner). The first answer mentions that Scanners are regex based, which agrees with the comments you already have on this question. – Rainbolt Sep 26 '14 at 14:52
  • `^[a-zA-Z][0-9]{6}[a-zA-Z]$` – Sourav 'Abhi' Mitra Sep 26 '14 at 14:57
  • @Robert I corrected it, please check –  Oct 21 '14 at 09:10

2 Answers2

3

Use Java regular expression:

public static void main(String[] args) {

    Pattern p = Pattern.compile("[a-zA-Z][0-9]{6}[a-zA-Z]");

    System.out.println(p.matcher("L123456X").matches()); // true
    System.out.println(p.matcher("L12345X").matches()); // false

    System.out.println(p.matcher("1234567X").matches()); // false
    System.out.println(p.matcher("A1234567X").matches()); // false
}

If you want uppercase letters only. Use [A-Z][0-9]{6}[A-Z]

NOTE: We don't have to add ^ and $ as some developers often do (For java)

Loc
  • 8,364
  • 6
  • 36
  • 73
  • Your pattern matches a bit too much, eg. "shouldnotmatch123456X" is but should not be accepted. Abhi's comment on the OP rightly adds anchors (^ and $) to that pattern. – hiergiltdiestfu Sep 26 '14 at 14:59
  • @hiergiltdiestfu: We don;t have to add ^ and $. You can test it. If you pass shouldnotmatch123456X || 123456X || 1234567X -- > return false – Loc Sep 26 '14 at 15:02
  • You are right. When testing with the Pattern class, it returns false as intended. When writing the comment, I had crosschecked your pattern with Rubular, where it returned a match. Guess that's something to keep in mind. I'd still say that it's cleaner and closer to the letters of the OP's rules if you'd use anchors. – hiergiltdiestfu Sep 26 '14 at 15:06
1

Yes, you need to look at regular expressions. All of your rules can be expressed in such expressions. Once you get the hang of that special syntax, you'll love it. Here's a beginners guide. Use this to test your resulting expression.

Afterwards, you can test your input in a single statement by asking

Pattern.matches($your_expression, str);
hiergiltdiestfu
  • 2,151
  • 2
  • 22
  • 34