0

I am trying to check an input String: - length - type - special char at the end

The input is a identity card like this 24659213Q.

So what I got for now is:

    public void datosUsuario() {
    System.out.print("Write ID: ");
    input = scanner.nextLine();
}


//ID check
public void comprobacion() {
    System.out.println("Checking ID length...");
    if (input.length() == 9){
        status = true;
        System.out.println("Length: OK!");
    } else {
        System.out.println("Length not OK! Try again!\n");
        status = false;
    }
}

So I am checking the entire String for having 8+1 length and now I am having problems checking if it has 8 digits and a char at the end of the input.

Any ideas would be apreciated! Thank you!

ALx ALx
  • 111
  • 7

5 Answers5

3

I'd use a regular expression:

String input = scanner.nextLine();
input.matches("/^[0-9]{8}[A-Za-z]$/);

See String.matches and regular expression documentation.

Moose Morals
  • 1,628
  • 25
  • 32
  • Can it be adapted so that the last alphabet character matches a char list like the next: `static final char[] LETTER = {'T', 'R', 'W', 'A', 'G', 'M', 'Y', 'F', 'P', 'D', 'X' ,'B', 'N', 'J', 'Z', 'S', 'Q', 'V', 'H', 'L', 'C' , 'K', 'E'};` – ALx ALx Sep 25 '16 at 10:22
  • Sure. Change `[A-Za-z]` to `[TRWAGMYFPDXBNJZSQVHLCKE]`, and read up on regular expression character classes. – Moose Morals Sep 25 '16 at 10:33
  • That works but it asks me to check it from a static constant defined at the start of my java file. So would something like this work? `input.matches("/^[0-9]{8}+indexOf(LETTER)$/");` – ALx ALx Sep 25 '16 at 10:37
  • 1
    I'd suggest researching dynamic regular expressions. Example question http://stackoverflow.com/questions/10172753/adding-variables-in-a-regular-expression-to-make-it-dynamic-in-java – Moose Morals Sep 25 '16 at 10:41
1

A simple method would be:

//ID check
public void comprobacion() {
System.out.println("Checking ID length...");
if (input.length() == 9) {
    if (Character.isAlphabetic(input.charAt(8)) {
        status = true;
        System.out.println("OK!");
    } else {
        status = false;
        System.out.println("Length: OK, but last character must be alphabetic");
    }
} else {
    System.out.println("Length not OK! Try again!\n");
    status = false;
}
baltzar
  • 446
  • 4
  • 8
1

You can use reg ex,

  public static void comprobacion(String input) {
    status = false;
    if(input.matches("\\d{8}\\w{1}"))
    {
      status = true;
    }

  }

Here, \d{8} = eight digits \w{1} = one alphabetical character

JTeam
  • 1,455
  • 1
  • 11
  • 16
  • Can it be adapted so that the last alphabet character matches a char list like the next: static final char[] LETTER = {'T', 'R', 'W', 'A', 'G', 'M', 'Y', 'F', 'P', 'D', 'X' ,'B', 'N', 'J', 'Z', 'S', 'Q', 'V', 'H', 'L', 'C' , 'K', 'E'}; – ALx ALx Sep 25 '16 at 10:19
  • input.matches("\\d{8}[T, R, W, A, G, M, Y, F, P, D, X ,B, N, J, Z, S, Q, V, H, L, C , K, E]{1}") This should work. – JTeam Oct 02 '16 at 10:14
0

You could use "Character.isDigit()" to determine if a character is a digit or not. In other words, you could create a for loop to interate through each character, checking whether it is a digit or not. Here's an example:

String input = "24659213Q";
for(int c = 0; c < input.length()-1; c++){
    //Checks all but the last character
    if( !Character.isDigit( input.charAt(c) ) ){
        System.out.println("The String does not start with 8 digits");
    }
}
if( Character.isDigit( input.charAt(8) ) ){
    //Checks only last character
    System.out.println("The String does not end with a char");
}
0

The method of regular expression can also be followed as mentioned above. There is one more way to do it.

1)Split the string into two -one with 8 characters and the other with last one character.

2)Parse the first String Integer.parseInt(subStringOfFirst8Char) in a try catch block catching NUmberFormatException.

If you don't catch the exception it is alright else it is wrong.

Shweta Gulati
  • 566
  • 1
  • 7
  • 17