Código de Java - Validador Tipo de Identificacion Java 2020

Requerimientos

JAVA

2020

Publicado el 20 de Febrero del 2020gráfica de visualizaciones de la versión: 2020
2.669 visualizaciones desde el 20 de Febrero del 2020
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/**
 * Nombre : Validador de RUC / CEDULA
 * Version : 2.0
 * Fecha 10 / 02 /2020
 * Autor : Francesco Villacis
 */
 
public class ValidaTipoIdentificacionX {
 
    public boolean ValidaIdentifiacion(String strTipoDeIdentificacion) {
 
        if (strTipoDeIdentificacion != null) {
 
            if (strTipoDeIdentificacion.length() == 10) {
 
                return validadorDeCedula(strTipoDeIdentificacion);
 
            } else if (strTipoDeIdentificacion.length() == 13) {
 
                return ValidarRuc(strTipoDeIdentificacion);
 
            }
        }
 
        return false;
    }
 
    public boolean validadorDeCedula(String strTipoDeIdentificacion) {
 
        // Verifica Que sea mayor la cantidad de numeros no debe ser diferente a 10
        if (strTipoDeIdentificacion.length() != 10) {
            return false;
        } else {
 
            // Substring que lee los dos primeros digitos de la cedula
            int provincia = Integer.parseInt(strTipoDeIdentificacion.substring(0, 2));
 
            // Verifica que no sean mayor a 24 ni menor que 0
            if ((provincia > 24) && (provincia < 0)) {
                return false;
            } else {
 
                // Substring que lee el tercerDigito de la Verificacion
                int tercerD = Integer.parseInt(strTipoDeIdentificacion.substring(2, 3));
 
                if ((tercerD > 6)) {
 
                    return false;
 
                } else {
 
                    String primerD = strTipoDeIdentificacion.substring(0, 1);
                    String SegundoD = strTipoDeIdentificacion.substring(1, 2);
                    String TercerD = strTipoDeIdentificacion.substring(2, 3);
                    String CuartoD = strTipoDeIdentificacion.substring(3, 4);
                    String CincoD = strTipoDeIdentificacion.substring(4, 5);
                    String SeisD = strTipoDeIdentificacion.substring(5, 6);
                    String SieteD = strTipoDeIdentificacion.substring(6, 7);
                    String OchoD = strTipoDeIdentificacion.substring(7, 8);
                    String NueveD = strTipoDeIdentificacion.substring(8, 9);
 
                    int[] imp = new int[4];
 
                    imp[0] = Integer.parseInt(SegundoD) * 1;
 
                    imp[1] = Integer.parseInt(CuartoD) * 1;
 
                    imp[2] = Integer.parseInt(SeisD) * 1;
 
                    imp[3] = Integer.parseInt(OchoD) * 1;
 
                    int SumaI = imp[0] + imp[1] + imp[2] + imp[3];
 
                    int[] par = new int[5];
                    par[0] = Integer.parseInt(primerD) * 2;
 
                    if (par[0] > 9) {
 
                        par[0] = par[0] - 9;
 
                    }
 
                    par[1] = Integer.parseInt(TercerD) * 2;
 
                    if (par[1] > 9) {
 
                        par[1] = par[1] - 9;
 
                    }
 
                    par[2] = Integer.parseInt(CincoD) * 2;
 
                    if (par[2] > 9) {
 
                        par[2] = par[2] - 9;
 
                    }
                    par[3] = Integer.parseInt(SieteD) * 2;
 
                    if (par[3] > 9) {
 
                        par[3] = par[3] - 9;
 
                    }
                    par[4] = Integer.parseInt(NueveD) * 2;
 
                    if (par[4] > 9) {
                        par[4] = par[4] - 9;
                    }
 
                    int SumaP = par[0] + par[1] + par[2] + par[3] + par[4];
 
                    int SumaT = SumaI + SumaP;
 
                    int S = SumaT % 10;
 
                    if (S > 0) {
 
                        S = 10 - S;
 
                    }
                    int UltimoD = Integer.parseInt(strTipoDeIdentificacion.substring(9));
                    //SI LA RESTA ES IGUAL AL DIGITO VERIFICADOR, SE VALIDA, 0 , SI EL RESIDUO DA 0 , SE VALIDA TAMBIEN
                    if (S == UltimoD || S == 0) {
 
                        return true;
                    }
 
                }
 
            }
            return false;
        }
 
    }
 
    public boolean ValidarRuc(String strTipoDeIdentificacion) {
 
        // Se verifica que solo pueden haber 13 digitos
        if (strTipoDeIdentificacion.length() != 13) {
            return false;
        } else {
 
            // Se leen los dos primeros digitos de el ruc
            int provincia = Integer.parseInt(strTipoDeIdentificacion.substring(0, 2));
 
            // Se Verifica que los dos primeros digitos no pueden ser mayores a 24
            if ((provincia > 24) && (provincia < 0)) {
 
                return false;
 
            } else {
 
            }
            // Se lee solamente el tercer digito de el ruc
            int tercerDigito = Integer.parseInt(strTipoDeIdentificacion.substring(2, 3));
 
            // Se verifica que no sea mayor a 9 ni menor a 6
            if ((tercerDigito > 7 && (tercerDigito) != 9)) {
 
                return false;
 
            }
 
            int UltimosDigitos1 = Integer.parseInt(strTipoDeIdentificacion.substring(10, 11));
 
            int UltimoDigito2 = Integer.parseInt(strTipoDeIdentificacion.substring(11, 12));
 
            int UltimoDigito3 = Integer.parseInt(strTipoDeIdentificacion.substring(12, 13));
 
            if (UltimosDigitos1 != 0) {
 
                return false;
 
            } else if (UltimoDigito2 != 0) {
 
                return false;
 
            } else if (UltimoDigito3 != 1) {
 
                return false;
            }
 
            // Se leen todos los digitos menos el ultimo, el cual es el verificador
 
            if (tercerDigito == 9) {
 
                String primerD = strTipoDeIdentificacion.substring(0, 1);
 
                String SegundoD = strTipoDeIdentificacion.substring(1, 2);
 
                String TercerD = strTipoDeIdentificacion.substring(2, 3);
 
                String CuartoD = strTipoDeIdentificacion.substring(3, 4);
 
                String CincoD = strTipoDeIdentificacion.substring(4, 5);
 
                String SeisD = strTipoDeIdentificacion.substring(5, 6);
 
                String SieteD = strTipoDeIdentificacion.substring(6, 7);
 
                String OchoD = strTipoDeIdentificacion.substring(7, 8);
 
                String NueveD = strTipoDeIdentificacion.substring(8, 9);
 
                int[] operaciones = new int[9];
 
                operaciones[0] = Integer.parseInt(primerD) * 4;
 
                operaciones[1] = Integer.parseInt(SegundoD) * 3;
 
                operaciones[2] = Integer.parseInt(TercerD) * 2;
 
                operaciones[3] = Integer.parseInt(CuartoD) * 7;
 
                operaciones[4] = Integer.parseInt(CincoD) * 6;
 
                operaciones[5] = Integer.parseInt(SeisD) * 5;
 
                operaciones[6] = Integer.parseInt(SieteD) * 4;
 
                operaciones[7] = Integer.parseInt(OchoD) * 3;
 
                operaciones[8] = Integer.parseInt(NueveD) * 2;
 
                int Suma = operaciones[0] + operaciones[1] + operaciones[2] + operaciones[3] +
                        operaciones[4] + operaciones[5] + operaciones[6]
                        + operaciones[7] + operaciones[8];
 
                int Residuo = Suma % 11;
 
                int Resta = 11 - Residuo;
 
                String DigitoVerificador = strTipoDeIdentificacion.substring(9, 10);
 
                //SI LA RESTA ES IGUAL AL DIGITO VERIFICADOR, SE VALIDA, 0 , SI EL RESIDUO DA 0 , SE VALIDA TAMBIEN
                if (Resta == Integer.parseInt(DigitoVerificador) || Residuo == 0) {
 
                    return true;
 
                }
 
            } else if (tercerDigito == 6 || tercerDigito < 6) {
 
                String primerD = strTipoDeIdentificacion.substring(0, 1);
 
                String SegundoD = strTipoDeIdentificacion.substring(1, 2);
 
                String TercerD = strTipoDeIdentificacion.substring(2, 3);
 
                String CuartoD = strTipoDeIdentificacion.substring(3, 4);
 
                String CincoD = strTipoDeIdentificacion.substring(4, 5);
 
                String SeisD = strTipoDeIdentificacion.substring(5, 6);
 
                String SieteD = strTipoDeIdentificacion.substring(6, 7);
 
                String OchoD = strTipoDeIdentificacion.substring(7, 8);
 
                String NueveD = strTipoDeIdentificacion.substring(8, 9);
 
 
                int[] operaciones = new int[8];
                operaciones[0] = Integer.parseInt(primerD) * 3;
 
                operaciones[1] = Integer.parseInt(SegundoD) * 2;
 
                operaciones[2] = Integer.parseInt(TercerD) * 7;
 
                operaciones[3] = Integer.parseInt(CuartoD) * 6;
 
                operaciones[4] = Integer.parseInt(CincoD) * 5;
 
                operaciones[5] = Integer.parseInt(SeisD) * 4;
 
                operaciones[6] = Integer.parseInt(SieteD) * 3;
 
                operaciones[7] = Integer.parseInt(OchoD) * 2;
 
                int Suma = operaciones[0] + operaciones[1] + operaciones[2]
                        + operaciones[3] + operaciones[4] + operaciones[5]
                        + operaciones[6] + operaciones[7];
 
                int Residuo = Suma % 11;
 
                int Resta = 11 - Residuo;
 
 
                    // Se lee el ultimo digito de la cedula
                String DigitoVerificador = strTipoDeIdentificacion.substring(9, 10);
 
                //SI LA RESTA ES IGUAL AL DIGITO VERIFICADOR, SE VALIDA, 0 , SI EL RESIDUO DA 0 , SE VALIDA TAMBIEN
                if (Resta == Integer.parseInt(DigitoVerificador) || Residuo == 0) {
                    return true;
 
                } else {
 
                    int[] imp = new int[4];
 
                    imp[0] = Integer.parseInt(SegundoD) * 1;
 
                    imp[1] = Integer.parseInt(CuartoD) * 1;
 
                    imp[2] = Integer.parseInt(SeisD) * 1;
 
                    imp[3] = Integer.parseInt(OchoD) * 1;
 
                     // Se suman todos los arreglos
                    int SumaI = imp[0] + imp[1] + imp[2] + imp[3];
 
                    // Se crea otro arreglo para meter todos los pares y multiplicarlos por dos
                    int[] par = new int[5];
 
 
 
                    /*
		     Se realiza la condicion, si la multiplicacion realizada es mayor a 9 Entonces
		     se le resta 9
                     */
 
                     par[0] = Integer.parseInt(primerD) * 2;
 
                    if (par[0] > 9) {
 
                        par[0] = par[0] - 9;
 
                    }
 
                    par[1] = Integer.parseInt(TercerD) * 2;
 
                    if (par[1] > 9) {
 
                        par[1] = par[1] - 9;
 
                    }
 
                    par[2] = Integer.parseInt(CincoD) * 2;
 
                    if (par[2] > 9) {
 
                        par[2] = par[2] - 9;
 
                    }
                    par[3] = Integer.parseInt(SieteD) * 2;
 
                    if (par[3] > 9) {
 
                        par[3] = par[3] - 9;
 
                    }
                    par[4] = Integer.parseInt(NueveD) * 2;
 
                    if (par[4] > 9) {
 
                        par[4] = par[4] - 9;
                    }
 
                    // Se suman todos los pares
                    int SumaP = par[0] + par[1] + par[2] + par[3] + par[4];
 
                    // Se suman los pares e impares
                    int SumaT = SumaI + SumaP;
 
 
                     // Se le saca el residuo a la suma
                    int S = SumaT % 10;
 
 
                     // Si el residuo es mayor a 0, se le resta 10
                    if (S > 0) {
 
                        S = 10 - S;
 
                    }
 
                    // Se lee el ultimo digito de la cedula
                    int UltimoD = Integer.parseInt(strTipoDeIdentificacion.substring(9, 10));
 
 
                    //SI LA RESTA ES IGUAL AL DIGITO VERIFICADOR, SE VALIDA, o , SI EL RESIDUO DA 0 , SE VALIDA TAMBIEN
                    if (S == UltimoD || S == 0) {
 
                        return true;
 
                    } else {
 
                        return false;
                    }
 
                }
            }
        }
        return false;
    }
 
}



Comentarios sobre la versión: 2020 (0)


No hay comentarios
 

Comentar la versión: 2020

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s5947