xbug 0 Denunciar post Postado Abril 16, 2010 Bom pessoal, proucurei no forum sobre validação de CNPJ e nao encontrei, em alguns outros lugares encontrei mas nao intendi muito bem a dinamica. Eu tentei criar um aqui mas so estou conseguindo retornar false. Alguem poderia analizar meu codigo? /** * Método validarCNPJ - Validação do DV do CNPJ * @param cnpj String com o CNPJ * @return true para CNPJ com dv correto */ public static boolean validaCNPJ(String CNPJ){ if (CNPJ.length() != 14 ) return false; int T1 = 0, T2=0; int j=9; int h=5; int x1, x2; String DV, xy; for (int i=0; i<4; i++){ T1+= h * Integer.parseInt(CNPJ.substring(i, i+1)); T2+= (h+1) * Integer.parseInt(CNPJ.substring(i, i+1)); h--; } T2+= (2 * Integer.parseInt(CNPJ.substring(h-1, h))); for (int i = 4 ; i<11; i++){ T1+= (j) * Integer.parseInt(CNPJ.substring(i, i+1)); T2+= (j) * Integer.parseInt(CNPJ.substring(i+1, i+2)); j--; } x1 = 11 - (T1 % 11); if(x1 > 9) x1 = 0; T2 += (2 * x1); x2 = 11 - (T2 % 11); if (x2 > 9) x2 = 0; DV = String.valueOf(x1) + String.valueOf(x2); xy = CNPJ.substring(CNPJ.length() - 2); return DV.equals(xy); } Compartilhar este post Link para o post Compartilhar em outros sites
Giovanni Ritchie 0 Denunciar post Postado Abril 16, 2010 Ja ouviu falar na BrazilUtils?? Uma API com muitas classes utilitarias que vao facilitar seu trabalho, da uma pesquisada por aí... []s Compartilhar este post Link para o post Compartilhar em outros sites
xbug 0 Denunciar post Postado Abril 20, 2010 Ja ouviu falar na BrazilUtils?? Uma API com muitas classes utilitarias que vao facilitar seu trabalho, da uma pesquisada por aí... []s pois é.. mas eu to fazendo este codigo para aprender mesmo! mas vou proucurar aqui pra ver se da uma ajudada tbm Compartilhar este post Link para o post Compartilhar em outros sites