Validação de CPF

1 resposta
A

Aceito sugestões de melhorias e resultados de testes…

/*13/04/2004
 *Esta classe implementa a validação dos dígitos verificadores do CPF
 */

 import java.io.*;
 public class ValidaCPF {
 	public static void main(String args[]) {
 		boolean continua = true;
 		try {
	 		BufferedReader dado = new BufferedReader(new InputStreamReader(System.in));
		 	String s= "";
		 	System.out.print("digite o cpf: ");
		 	s = dado.readLine();
			int mult = 10;
	 		int marc = 9;
	 		while (continua && marc <11) {
	 			int pos = 0;
	 			int dig = 0;
	 			while (pos <=marc-1) {
	 				dig = dig + (Integer.parseInt(s.substring(pos,pos+1))*mult);
	 				pos++;
	 				mult--;
	 			}
	 			if (dig%11 > 0) {
	 				if (11 - (dig%11) == Integer.parseInt(s.substring(marc,marc+1))) {
	 					mult = 11;
	 				}
	 				else {
	 					continua = false;
	 				}
	 			}
	 			else {
	 				if (Integer.parseInt(s.substring(marc,marc+1))==0) {
	 					mult = 11;
	 				}
	 				else {
	 					continua = false;
	 				}
	 			}
	 		marc++;
	 		}
 		}
 		catch(IOException e) {
 			System.out.println("erro na Entrada");
 		}
 		catch(IndexOutOfBoundsException e) {
 			System.out.println("CPF digitado no formato errado");
 			System.exit(0);
 		}
 		if(continua) {
 			System.out.println("CPF Valido");
 		}
 		else {
 			System.out.println("CPF Invalido");
 		}
 	}
 }

[/code]

1 Resposta

W

Dá uma olhada como a Diana fez:
http://www.portaljava.com.br/home/modules.php?name=Forums&file=viewtopic&t=3682&sid=a96d20a3aadc9753950918d14a383f2a

Criado 19 de abril de 2004
Ultima resposta 19 de abr. de 2004
Respostas 1
Participantes 2