How to put a secure java license [closed]

0

Hi, I have to do registration (it will be composed of 4 consecutive numbers followed by 3 consecutive letters, without blank spaces between numbers and letters). And I want a method that receives a string enrollment and check that it has an appropriate format

I have done,

 private final String matricula ="1111AAA";

Would that be fine? Or would you create a method?

    
asked by kitkat 07.02.2017 в 10:02
source

3 answers

4

You can use a regular expression:

  

^[0-9]{4}[A-Z]{3}$

Means that it only supports 4 digits ( [0-9]{4} ) followed by 3 letters ( [A-Z]{3} )

boolean comprobarMatricula (String matricula){

    if (matricula.toUpperCase().matches("^[0-9]{4}[A-Z]{3}$")) {
        System.out.println("Matrícula válida");
        return true;

    }else{

        System.out.println("Matrícula inválida");
        return false;
    }      
}
    
answered by 07.02.2017 в 10:15
3

Here you would have a method to generate a valid license plate:

private static String generaMatricula()
  {
    //Letras válidas para matrícula
    char[] array = {'B', 'C',
                    'D', 'F', 'G', 'H', 'J', 'K', 'L', 
                    'M', 'N', 'P', 'R', 'S', 'T', 'V', 
                    'W', 'X', 'Y', 'Z'};

    String matricula = "";

    for (int i=0; i<7; i++){
      Random rnd = new Random();
      int ale = (int)(rnd.nextDouble() * array.length); //Aleatorio para la letra
      int ale2 = (int)(rnd.nextDouble() * 10); //Aleatorio entre 0-9
      if (i>3) {
        matricula += array[ale];
      } else {
        matricula +=  ale2;
      }
    }

    return matricula;

  }

Being able to call it in the following way:

System.out.println(generaMatricula());
    
answered by 07.02.2017 в 10:14
1

Do you want to check if the registration is valid? For that we can create a method that checks through regular expressions if the registration is correct.

We must bear in mind that there is a European format and Old Spanish, for this we can use the following method that checks both cases or, if you are only interested in the European method, you can do a return false in the else of the first if

    final String consonantes = "BCDFGHIJKLMNPQRSTUVWXYZ";
    final Pattern patternES = Pattern.compile("(["+consonantes+"]{1,2})(\d{4})(["+consonantes+"]{0,2})");
    final Pattern patternEU = Pattern.compile("(\d{4})(["+consonantes+"]{3})");
    boolean checkMatricula(String matricula){
            matricula = matricula.toUpperCase(); 
            Matcher eu = patternEU.matcher(string);
            if(eu.groupCount()>1){
                 return true;
            }else {
                Matcher es = patternES.matcher(string);
                if(es.groupCount()>1){
                    return true;
                }else {
                    return false;
                }
           }
    }

Here you can also use the matcher to return the groups that are there, in the case of ES we have group 0 (all enrollment), 1 (first letters), 2 (numbers) and 3 (last letters) while the EU has the 0 (all enrollment), 1 (numbers) and 2 (letters)

    
answered by 07.02.2017 в 10:36