Eis os dois exemplos com fontes!

------------
class CheckCPF
{

public static boolean TestaCpf(String xCPF)

  //Testa se o CPF é válido ou não
   int d1,d4,xx,nCount,resto,digito1,digito2;
   String Check;
  String Separadores = "/-.";
   d1 = 0; d4 = 0; xx = 1;
   for (nCount = 0; nCount < xCPF.length() -2; nCount++) {
    String s_aux = xCPF.substring(nCount, nCount+1);
   System.out.println(s_aux);
      if (Separadores.indexOf(s_aux) == -1) {
          d1 = d1 + ( 11 - xx ) * Integer.valueOf (s_aux).intValue();
          d4 = d4 + ( 12 - xx ) * Integer.valueOf (s_aux).intValue();
          xx++;
      };
   };
   resto = (d1 % 11);
   if (resto < 2)
      digito1 = 0;
   else
      digito1 = 11 - resto;

   d4 = d4 + 2 * digito1;
   resto = (d4 % 11);
   if (resto < 2)
      digito2 = 0;
   else
      digito2 = 11 - resto;

   Check = String.valueOf(digito1) + String.valueOf(digito2);

 String s_aux2 = xCPF.substring (xCPF.length()-2, xCPF.length());
  System.out.println(s_aux2);
 System.out.println(Check);

 if (s_aux2.compareTo (Check) != 0)
      return false;
  return true;
};
 public static void main (String args[]) {
   if (!(TestaCpf ("020389239-90")))
     System.out.println("invalido");
   else
     System.out.println("valido");
   if (!(TestaCpf ("020389239/90")))
     System.out.println("invalido");
   else
     System.out.println("valido");
   if (!(TestaCpf ("020389239.90")))
     System.out.println("invalido");
   else
     System.out.println("valido");
   if (!(TestaCpf ("02038923990")))
     System.out.println("invalido");
   else
     System.out.println("valido");
 }

}

------------
class CheckCGC
{

public static boolean TestaCgc(String xCGC) {
   //Testa se o CGC é válido ou não
   int d1,d4,xx,nCount,fator,resto,digito1,digito2;
   String Check, s_aux;
   String Separadores = "/-.";
   d1 = 0;
   d4 = 0;
   xx = 0;
   for (nCount = 0; nCount < xCGC.length()-2; nCount++) {
    s_aux = xCGC.substring (nCount, nCount+1);
    if (Separadores.indexOf(s_aux) == -1) {
         if (xx < 4)
            fator = 5 - xx;
         else
            fator = 13 - xx;
         d1 = d1 + Integer.valueOf (s_aux).intValue() * fator;
         if (xx < 5)
            fator = 6 - xx;
         else
            fator = 14 - xx;
         d4 += Integer.valueOf (s_aux).intValue() * fator;
         xx++;
      };
  }
   resto = (d1 % 11);
   if (resto < 2)
      digito1 = 0;
   else
      digito1 = 11 - resto;

   d4 = d4 + 2 * digito1;
   resto = (d4 % 11);
   if (resto < 2)
      digito2 = 0;
   else
       digito2 = 11 - resto;

   Check = String.valueOf(digito1) + String.valueOf(digito2);
  System.out.println (Check);
  System.out.println (xCGC.substring(xCGC.length()-2, xCGC.length() ));
   if (Check.compareTo(xCGC.substring(xCGC.length()-2, xCGC.length() )) !=
0)
      return false;

   return true;
}

 public static void main (String args[]) {

   //Este é um CGC válido
   if (!(TestaCgc ("76431279000120")))
     System.out.println("invalido");
   else
     System.out.println("valido");

   //Este é um CGC válido
   if (!(TestaCgc ("02174192000188")))
     System.out.println("invalido");
   else
     System.out.println("valido");

   //Este é um CGC inválido
   if (!(TestaCgc ("02038926575690")))
     System.out.println("invalido");
   else
     System.out.println("valido");

   //Este é um CGC inválido
   if (!(TestaCgc ("02038923996570")))
     System.out.println("invalido");
   else
     System.out.println("valido");
 }
}





-----Mensagem Original-----
De: Mauri Ferrandin <[EMAIL PROTECTED]>
Para: <[EMAIL PROTECTED]>
Enviada em: Sexta-feira, 21 de Julho de 2000 10:06
Assunto: [SouJava-J] CGC e CPF !!


> Alguem pode me enviar as rotinas para validar o cgc e cpf ou cnpj em
> java ?
>
> --
>
>
============================================================================
===============================
>
> Mauri Ferrandin - [EMAIL PROTECTED] Núcleo de Informática - Centro
> Universitário de Jaraguá do Sul - SC
> Linux registred user #121834 Registre-se de graca http://counter.li.org
>
>  "A fé remove montanhas, mas eu prefiro dinamite."
>
>
>     --------------------------- LISTA SOUJAVA ---------------------------
>     http://www.soujava.org.br  -  Sociedade de Usuários Java da Sucesu-SP
>     [dúvidas mais comuns: http://www.soujava.org.br/faq.htm]
>     [para sair da lista: http://www.soujava.org.br/forum/cadastrados.htm]
>     [regras da lista: http://www.soujava.org.br/regras.htm]
>     ---------------------------------------------------------------------
>

    --------------------------- LISTA SOUJAVA ---------------------------
    http://www.soujava.org.br  -  Sociedade de Usuários Java da Sucesu-SP
    [dúvidas mais comuns: http://www.soujava.org.br/faq.htm]
    [para sair da lista: http://www.soujava.org.br/forum/cadastrados.htm]
    [regras da lista: http://www.soujava.org.br/regras.htm]
    ---------------------------------------------------------------------

Responder a