Obs.: A funcao  completarValor n�o foi criada por mim logo n�o estou
mandando ela o unico motivo do uso dela e preencher com zeros a esquerda os
numeros e convertelos em string.

/// CNPJ
public final static String TESTACNPJ(long cnpj,int filial,int controle){
        if(verifyCNPJ(cnpj,filial,controle) == true)//1
                return "CNPJ v�lido!";
        else
                return "CNPJ n�o � v�lido!";
}
public static final boolean RealTestaCNPJ(String cnpj,int g){
        int VerCNPJ=0;
        int ind=2;
        int tam;
        StringBuffer bufferCNPJ= new StringBuffer();
        bufferCNPJ.append(cnpj);
        for(int f=g;f>0;--f)
                {
                VerCNPJ+=Integer.parseInt(bufferCNPJ.substring(f-1,f))*ind;
                if(ind>8)
                        {
                        ind=2;
                        }
                else
                        {
                        ind++;
                        }
                }
                VerCNPJ%=11;
                if(VerCNPJ==0 || VerCNPJ==1)
                        {
                        VerCNPJ=0;
                        }
                else
                        {
                        VerCNPJ=11-VerCNPJ;
                        }
        if(VerCNPJ !=Integer.parseInt(bufferCNPJ.substring(g,g+1)))
                {
                return false;//0
                }
        else
                {
                return true;//1
                }
        }

// Aqui inicia as fun��es de teste do CNPJ
public static final boolean verifyCNPJ(long cnpj,int filial,int controle){
                String strCNPJ=new String(
                                convertString(cnpj) +
        
completarValor(4,C_NUMERO,Integer.toString(filial)) +
        
completarValor(2,C_NUMERO,Integer.toString(controle))
                                                                );
                System.out.println(strCNPJ);
                int g;
                
                if(cnpj == 0)
                        return false;
                else {
                        g=strCNPJ.length()-2;
                        if(RealTestaCNPJ(strCNPJ,g) == true)//1
                                {
                                g=strCNPJ.length()-1;
                                if(RealTestaCNPJ(strCNPJ,g) == true)//1
                                        {       
                                        return true;//1
                                        }
                                else
                                        {
                                        return false;//0
                                        }
                                }
                        else
                                {
                                return false;//0
                                }
                        }
        
}
/**
 * 
 * @return java.lang.String
 * @param numero long
 */
public static final String convertString(long numero) {
        StringBuffer string = new StringBuffer();
        return string.append(numero).toString();
}

public static void main(String[] args) {
 System.out.print("52736949/0001-58");
 System.out.print(TESTACNPJ(52736949,0001,58));
 System.out.print("52736949/0001-57");  
 System.out.print(TESTACNPJ(52736949,0001,58)); 
        }
///////////////////////////////////////////

PS.:/ todo o codigo assima foi convertido do codigo abaixo em javascript
http://codigolivre.org.br/links/outros
Latest Snippet Version: 1.0

<html><head><title>Valida CNPJ v.1.0</title></head>

<script language="JavaScript">
function TESTA()
        {
        if(VerifyCNPJ(document.forms[0].CNPJ.value) == 1)
                {
                alert("CNPJ v�lido!");
                }
        else
                {
                alert("CNPJ n�o � v�lido!");
                }
        document.forms[0].CNPJ.focus();
        return;
        }



// Aqui inicia as fun��es de teste do CNPJ


function isNUMB(c)
        {
        if((cx=c.indexOf(","))!=-1)
                {               
                c = c.substring(0,cx)+"."+c.substring(cx+1);
                }
        if((parseFloat(c) / c != 1))
                {
                if(parseFloat(c) * c == 0)
                        {
                        return(1);
                        }
                else
                        {
                        return(0);
                        }
                }
        else
                {
                return(1);
                }
        }

function LIMP(c)
        {
        while((cx=c.indexOf("-"))!=-1)
                {               
                c = c.substring(0,cx)+c.substring(cx+1);
                }
        while((cx=c.indexOf("/"))!=-1)
                {               
                c = c.substring(0,cx)+c.substring(cx+1);
                }
        while((cx=c.indexOf(","))!=-1)
                {               
                c = c.substring(0,cx)+c.substring(cx+1);
                }
        while((cx=c.indexOf("."))!=-1)
                {               
                c = c.substring(0,cx)+c.substring(cx+1);
                }
        while((cx=c.indexOf("("))!=-1)
                {               
                c = c.substring(0,cx)+c.substring(cx+1);
                }
        while((cx=c.indexOf(")"))!=-1)
                {               
                c = c.substring(0,cx)+c.substring(cx+1);
                }
        while((cx=c.indexOf(" "))!=-1)
                {               
                c = c.substring(0,cx)+c.substring(cx+1);
                }
        return(c);
        }

function VerifyCNPJ(CNPJ)
        {
        CNPJ = LIMP(CNPJ);
        if(isNUMB(CNPJ) != 1)
                {
                return(0);
                }
        else
                {
                if(CNPJ == 0)
                        {
                        return(0);
                        }
                else
                        {
                        g=CNPJ.length-2;
                        if(RealTestaCNPJ(CNPJ,g) == 1)
                                {
                                g=CNPJ.length-1;
                                if(RealTestaCNPJ(CNPJ,g) == 1)
                                        {       
                                        return(1);
                                        }
                                else
                                        {
                                        return(0);
                                        }
                                }
                        else
                                {
                                return(0);
                                }
                        }
                }
        }
function RealTestaCNPJ(CNPJ,g)
        {
        var VerCNPJ=0;
        var ind=2;
        var tam;
        for(f=g;f>0;f--)
                {
                VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind;
                if(ind>8)
                        {
                        ind=2;
                        }
                else
                        {
                        ind++;
                        }
                }
                VerCNPJ%=11;
                if(VerCNPJ==0 || VerCNPJ==1)
                        {
                        VerCNPJ=0;
                        }
                else
                        {
                        VerCNPJ=11-VerCNPJ;
                        }
        if(VerCNPJ!=parseInt(CNPJ.charAt(g)))
                {
                return(0);
                }
        else
                {
                return(1);
                }
        }
</script>

<body><p align=justify>Este � um modelo de c�digo JavaScript que faz a
valida��o de um n�mero de CNPJ (o antigo CGC) brasileiro.</p>

<form action='javascript: TESTA()'>Digite aqui o CNPJ: <input type='text'
name='CNPJ' onFocus='this.select()'> <input type='button' value='VALIDA'
onClick='TESTA();'></form><br><br>

<p align=justify>Para um melhor funcionamento, o c�digo da valida��o do CNPJ
foi dividido da seguinte forma:</p>
<p align=justify><font color=red>Fun��o LIMP</font>: Remove da string
caracteres de aux�lio, tipo ".", "/" entre outros, que aux�liam a
visualiza��o e digita��o dos dados pelos humanos. Acho deprimente, quando
entro num site e recebo uma respota do tipo "valor inv�lido", porque
coloquei um "-" no n�mero do CEP, por exemplo.</p>
<p align=justify><font color=red>Fun��o isNUMB</font>: Testa a string a fim
de detectar se � composta apenas de n�meros.</p>
<p align=justify>O CNPJ � testado em blocos, por isso, para um melhor
aproveitamento e redu��o do c�digo principal, subdividi o teste em duas
partes:</p>
<p align=justify><font color=red>VerifyCNPJ</font>: Verifica o CNPJ,
repassando os blocos para a outra fun��o RealTestaCNPJ. Se algum dos blocos
for inv�lido, a fun��o retorna como valor inv�lido.</p>
<p align=justify><font color=red>RealTestaCNPJ</font>: Verifica os blocos de
n�meros do CNPJ.</p>
<p align=justify>A transcri��o das fun��es pertinentes:</p>




<b><pre>

function isNUMB(c)
        {
        if((cx=c.indexOf(","))!=-1)
                {               
                c = c.substring(0,cx)+"."+c.substring(cx+1);
                }
        if((parseFloat(c) / c != 1))
                {
                if(parseFloat(c) * c == 0)
                        {
                        return(1);
                        }
                else
                        {
                        return(0);
                        }
                }
        else
                {
                return(1);
                }
        }

function LIMP(c)
        {
        while((cx=c.indexOf("-"))!=-1)
                {               
                c = c.substring(0,cx)+c.substring(cx+1);
                }
        while((cx=c.indexOf("/"))!=-1)
                {               
                c = c.substring(0,cx)+c.substring(cx+1);
                }
        while((cx=c.indexOf(","))!=-1)
                {               
                c = c.substring(0,cx)+c.substring(cx+1);
                }
        while((cx=c.indexOf("."))!=-1)
                {               
                c = c.substring(0,cx)+c.substring(cx+1);
                }
        while((cx=c.indexOf("("))!=-1)
                {               
                c = c.substring(0,cx)+c.substring(cx+1);
                }
        while((cx=c.indexOf(")"))!=-1)
                {               
                c = c.substring(0,cx)+c.substring(cx+1);
                }
        while((cx=c.indexOf(" "))!=-1)
                {               
                c = c.substring(0,cx)+c.substring(cx+1);
                }
        return(c);
        }

function VerifyCNPJ(CNPJ)
        {
        CNPJ = LIMP(CNPJ);
        if(isNUMB(CNPJ) != 1)
                {
                return(0);
                }
        else
                {
                if(CNPJ == 0)
                        {
                        return(0);
                        }
                else
                        {
                        g=CNPJ.length-2;
                        if(RealTestaCNPJ(CNPJ,g) == 1)
                                {
                                g=CNPJ.length-1;
                                if(RealTestaCNPJ(CNPJ,g) == 1)
                                        {       
                                        return(1);
                                        }
                                else
                                        {
                                        return(0);
                                        }
                                }
                        else
                                {
                                return(0);
                                }
                        }
                }
        }
function RealTestaCNPJ(CNPJ,g)
        {
        var VerCNPJ=0;
        var ind=2;
        var tam;
        for(f=g;f>0;f--)
                {
                VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind;
                if(ind>8)
                        {
                        ind=2;
                        }
                else
                        {
                        ind++;
                        }
                }
                VerCNPJ%=11;
                if(VerCNPJ==0 || VerCNPJ==1)
                        {
                        VerCNPJ=0;
                        }
                else
                        {
                        VerCNPJ=11-VerCNPJ;
                        }
        if(VerCNPJ!=parseInt(CNPJ.charAt(g)))
                {
                return(0);
                }
        else
                {
                return(1);
                }
        }

</pre></b>




</body></html>

                
Submit a new version </snippet/addversion.php?type=snippet&id=22>
You can submit a new version of this snippet if you have modified it and you
feel it is appropriate to share with others. 

Atenciosamente

Rodrigo Rocha - CPM SA
Analista em:
Qualidade e Sistemas da Informa��o
Linguagens JAVA,VB,Qarun e outras.
http://www.cpm.com.br
http://br.groups.yahoo.com/group/java_certification/files/banner.swf



------------------------------ 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