ai vai !

Jose Ferreira de Souza Filho wrote:

>Alguém tem algum método que valida CGC, ou o algoritmo que faça essa
>validação?
>
>
>Obrigado pela atenção de todos...
>
>
>Ferreira.
>
>------------------------------ 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
>regras da lista: http://www.soujava.org.br/regras.htm
>historico: http://www.mail-archive.com/java-list%40soujava.org.br
>para sair da lista: envie email para [EMAIL PROTECTED] 
>-------------------------------------------------------------------------
>

-- 

===========================================================================================================
Professor Mauri Ferrandin - [EMAIL PROTECTED] Núcleo(Kernel) de Informática 
UNERJ - Centro Universitário de Jaraguá do Sul - SC - Brazil
Linux registred user #121834 Registre-se de graca http://counter.li.org

"Os que podem se desfazer de liberdades essenciais para obter alguma segurança 
temporária, não merecem nem liberdade nem segurança". (Benjamin Franklin)




package rainha.Validacoes;

public class CGC_CPF
{

public static boolean validaCpf(String xCPF)
{
        try
        {
                
        
                //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;
        }
        catch (Exception e)
        {
                return false;           
        }
}


public static boolean validaCgc(String xCGC) 
{
        try
        {
                
                //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;
        }
        catch (Exception e)
        {
                return false;           
        }


}

/*
 
 public static void main (String args[]) {

          if ((validaCpf ("018581869/73")))
          {
                System.out.println("OK");
          }
          else
          {
                System.out.println("NOK");
          }

             //Este é um CGC válido
        if (!(validaCgc ("83130229/0001-78")))
     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 ("020389239.90")))
     System.out.println("invalido");
   else
     System.out.println("valido");
   if (!(TestaCpf ("02038923990")))
     System.out.println("invalido");
   else
     System.out.println("valido");

 }
 */

}


------------------------------ 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
regras da lista: http://www.soujava.org.br/regras.htm
historico: http://www.mail-archive.com/java-list%40soujava.org.br
para sair da lista: envie email para [EMAIL PROTECTED]
-------------------------------------------------------------------------

Responder a