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!

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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'};
Sure. Change [A-Za-z] to [TRWAGMYFPDXBNJZSQVHLCKE], and read up on regular expression character classes.
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)$/");
I'd suggest researching dynamic regular expressions. Example question stackoverflow.com/questions/10172753/…
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;
}

Comments

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

2 Comments

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'};
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.
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");
}

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.