How can I calculate the letter NIF of a DNI?

1

ENUNCIADO:

The algorithm to obtain the letter of the NIF corresponding to a DNI consists of the following steps:

  • Obtain the rest of the entire division of the DNI by 23.
  • The letter is the one in the indexed position of the following string, which corresponds the value of the previous waste: "TRWAGMYFPDXBNJZSQVHLCKE" example: the remainder of divide 22334455 between 23 is 6 and in position 6 of the previous chain is found the letter Y'. (The positions of the text strings start at 0).
  • Make a program that asks to enter DNI numbers until the user ends by entering a specific character (f | F).
  • DNI s must be stored in an array that starts with size 1 and must resize, increasing it in one position each time the user enters a new DNI.
  • The program must show the NIFs with their letters.

ENTRY OF DNI s:

22334455
45678965
12123256
45678964
45678987
12123233

DEPARTURE OF NIF s:

22334455Y
45678965E
12123256W
45678964K
45678987K
12123233W

After the attached statement what I have done: link

public static void main(String[] args) {

        Scanner lector = new Scanner(System.in);

        String[] letras = {"T", "R", "W", "A", "G", "M", "Y", "F", "P", "D", "X", "B", "N", "J", "Z", "S", "Q", "V", "H", "L", "C", "K", "E"};
        int[] valors = new int[5];
        int ocupacio = 0, i, x;
        int posicion;

        do {
            System.out.println("Introduce numeros de DNI");

            for (i = 0; i < valors.length; i++) {
                if (lector.hasNextInt()) {
                    valors[i] = lector.nextInt();

                } else if (lector.hasNext("fi")) {
                    break;

                } else {

                    System.out.println("Dada Incorrecta");
                    lector.nextLine();
                    i--;

                }

            }

            for (x = 0; x < letras.length; x++) {
                letras[x] = lector.next();

                ocupacio = i;
                posicion = x % 23;

                for (int j = 0; j < ocupacio; j++) {
                    System.out.println(valors[j]);

                    System.out.println("NIF: " + valors[j] + "-"+ letras[posicion]);
                }

            }

        } while (!lector.hasNext("fi"));

    }
}

I only have the last part that is to resize the array so that it shows me the letter next to the DNI that I enter and also not put together each letter with each DNI, I know that something has to be done with a counter but I do not know how do it and the truth is that I'm desperate, I hope you can give me a hand, many thanks from my heart.

    
asked by Aitor 13.02.2017 в 20:18
source

2 answers

1

Handle the digits entered in an array and create an array with size plus one with each entry is not very efficient, but I guess it is a task in which you must work with primitives. Do not get used to it.

import java.io.IOException;

public class Dni {

    public static final String[] letras = {"T", "R", "W", "A", "G", "M", "Y", "F", "P", "D", "X", "B", "N", "J", "Z", "S", "Q", "V", "H", "L", "C", "K", "E"};

    int[] valors = new int[0];

    public void ingreso() throws IOException{
        System.out.println("Introduce numeros de DNI");
        int indice=0;
        int consola;
        do{
            consola = System.in.read();
            // char ingresado es [0-9]
            if (consola >=48 && consola<=57){
                // crecer arreglo, agrega digito a arreglo y incrementa indice
                valors = crecerArreglo(valors);
                valors[indice++]=consola;
            }
            // 70 y 102 son [fF]
        } while(consola !=70 && consola !=102);
        String dni = arregloToString(valors);
        int nif = Integer.valueOf(dni)%23;
        System.out.println("Resultado:");
        System.out.println(dni.concat(letras[nif]));
    }

    private int[] crecerArreglo(int[] arreglo){
        int[] remplazo = new int[arreglo.length+1];
        // copiar arreglo
        for (int i = 0; i<arreglo.length; i++) remplazo[i]=arreglo[i];
        return remplazo;
    }

    private String arregloToString(int[] arreglo){
        char[] c = new char[arreglo.length];
        for (int i =0; i<arreglo.length;i++){
            c[i]=(char) (arreglo[i]);
        }
        return new String(c);
    }

    public static void main(String[] args) {
        Dni dni=new Dni();
        try {
            dni.ingreso();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
    
answered by 13.02.2017 / 21:51
source
0

In your exercise you had several errors of use of the Scanner class. And an important mess with the do-while and the for of its interior.

Here I leave the example that I have done using everything that is good of your code and leaving it as simple as possible

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner lector = new Scanner(System.in);

        String[] letras =
                          {"T", "R", "W", "A", "G", "M", "Y", "F", "P", "D", "X", "B", "N", "J", "Z", "S", "Q", "V", "H", "L", "C", "K",
                                  "E"};
        //Creamos el array con longitud 0 para ir ampliandolo como pide el enunciado.
        int[] valors = new int[0];
        int ocupacio = 0, i = 0, x;
        int posicion;
        boolean fi = false;

        System.out.println("Introduce numeros de DNI");
        do {
            if (lector.hasNextInt()) {
                int[] arrayAmpliado = new int[valors.length + 1];

                if(i > 0){
                    for (int j = 0; j < valors.length; j++) {
                        arrayAmpliado[j] = valors[j];
                    }
                }

                valors = arrayAmpliado;
                valors[i] = lector.nextInt();

            } else if (lector.hasNext()) {
                String s = lector.next();

                if ("f".equals(s) || "F".equals(s)) {
                    fi = true;
                }
            } else {

                System.out.println("Dada Incorrecta");
                lector.nextLine();
                i--;
            }

            i++;
        } while (!fi);

        for(int valor : valors){
            String letra = letras[valor % 23];

            System.out.println(valor + letra);
        }
    }
}

If you have any questions, we will comment on it!

    
answered by 15.02.2017 в 17:28