class Boleto {
public static void main
(String[] args
) { String codBarras
= "23793.44308.90010.000041.33001.250001.3.52830000008091"; String codBarrasLimpo
= codBarras.
replace(".",
""); int dv = Base10(codBarrasLimpo);
}
public static int Base10
(String num
){ //variáveis de instancia
int soma = 0;
int resto = 0;
int dv = 0;
int multiplicador = 2;
for (int i = num.length(); i > 0; i--) {
//Multiplica da direita pra esquerda, alternando os algarismos 2 e 1
if(multiplicador%2 == 0){
// pega cada numero isoladamente
numeros
[i
] = String.
valueOf(Integer.
valueOf(num.
substring(i
-1,i
))*2); multiplicador = 1;
}else{
numeros
[i
] = String.
valueOf(Integer.
valueOf(num.
substring(i
-1,i
))*1); multiplicador = 2;
}
}
// Realiza a soma dos campos de acordo com a regra
for(int i = (numeros.length-1); i > 0; i--){
if(aux.length()>1){
aux2 = aux.substring(0,aux.length()-1);
aux3 = aux.substring(aux.length()-1,aux.length());
}
else{
numeros[i] = aux;
}
}
//Realiza a soma de todos os elementos do array e calcula o digito verificador
//na base 10 de acordo com a regra.
for(int i = numeros.length; i > 0 ; i--){
if(numeros[i-1] != null){
soma
+= Integer.
valueOf(numeros
[i
-1]); }
}
resto = soma%10;
dv = 10 - resto;
//retorna o digito verificador
return dv;
}
}
Y2xhc3MgQm9sZXRvIHsKCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgU3RyaW5nIGNvZEJhcnJhcyA9ICIyMzc5My40NDMwOC45MDAxMC4wMDAwNDEuMzMwMDEuMjUwMDAxLjMuNTI4MzAwMDAwMDgwOTEiOyAgICAgICAgCiAgICAgICAgU3RyaW5nIGNvZEJhcnJhc0xpbXBvID0gY29kQmFycmFzLnJlcGxhY2UoIi4iLCAiIik7CiAgICAgICAgaW50IGR2ID0gQmFzZTEwKGNvZEJhcnJhc0xpbXBvKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oZHYpOwogICAgfQoKICAgICAgICBwdWJsaWMgc3RhdGljIGludCBCYXNlMTAoU3RyaW5nIG51bSl7ICAKICAgICAgICAgICAgLy92YXJpw6F2ZWlzIGRlIGluc3RhbmNpYQogICAgICAgICAgICBpbnQgc29tYSA9IDA7CiAgICAgICAgICAgIGludCByZXN0byA9IDA7CiAgICAgICAgICAgIGludCBkdiA9IDA7CiAgICAgICAgICAgIFN0cmluZ1tdIG51bWVyb3MgPSBuZXcgU3RyaW5nW251bS5sZW5ndGgoKSsxXTsKICAgICAgICAgICAgaW50IG11bHRpcGxpY2Fkb3IgPSAyOwogICAgICAgICAgICBTdHJpbmcgYXV4OwogICAgICAgICAgICBTdHJpbmcgYXV4MjsKICAgICAgICAgICAgU3RyaW5nIGF1eDM7CgogICAgICAgICAgICBmb3IgKGludCBpID0gbnVtLmxlbmd0aCgpOyBpID4gMDsgaS0tKSB7ICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAvL011bHRpcGxpY2EgZGEgZGlyZWl0YSBwcmEgZXNxdWVyZGEsIGFsdGVybmFuZG8gb3MgYWxnYXJpc21vcyAyIGUgMQogICAgICAgICAgICAgICAgaWYobXVsdGlwbGljYWRvciUyID09IDApewogICAgICAgICAgICAgICAgICAgIC8vIHBlZ2EgY2FkYSBudW1lcm8gaXNvbGFkYW1lbnRlICAKICAgICAgICAgICAgICAgICAgICBudW1lcm9zW2ldID0gU3RyaW5nLnZhbHVlT2YoSW50ZWdlci52YWx1ZU9mKG51bS5zdWJzdHJpbmcoaS0xLGkpKSoyKTsKICAgICAgICAgICAgICAgICAgICBtdWx0aXBsaWNhZG9yID0gMTsKICAgICAgICAgICAgICAgIH1lbHNlewogICAgICAgICAgICAgICAgICAgIG51bWVyb3NbaV0gPSBTdHJpbmcudmFsdWVPZihJbnRlZ2VyLnZhbHVlT2YobnVtLnN1YnN0cmluZyhpLTEsaSkpKjEpOwogICAgICAgICAgICAgICAgICAgIG11bHRpcGxpY2Fkb3IgPSAyOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9ICAKICAgICAgICAgICAgLy8gUmVhbGl6YSBhIHNvbWEgZG9zIGNhbXBvcyBkZSBhY29yZG8gY29tIGEgcmVncmEKICAgICAgICAgICAgZm9yKGludCBpID0gKG51bWVyb3MubGVuZ3RoLTEpOyBpID4gMDsgaS0tKXsKICAgICAgICAgICAgICAgIGF1eCA9IFN0cmluZy52YWx1ZU9mKEludGVnZXIudmFsdWVPZihudW1lcm9zW2ldKSk7CiAgICAgICAgICAgICAgICBpZihhdXgubGVuZ3RoKCk+MSl7CiAgICAgICAgICAgICAgICAgICAgYXV4MiA9IGF1eC5zdWJzdHJpbmcoMCxhdXgubGVuZ3RoKCktMSk7ICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgIGF1eDMgPSBhdXguc3Vic3RyaW5nKGF1eC5sZW5ndGgoKS0xLGF1eC5sZW5ndGgoKSk7CiAgICAgICAgICAgICAgICAgICAgbnVtZXJvc1tpXSA9IFN0cmluZy52YWx1ZU9mKEludGVnZXIudmFsdWVPZihhdXgyKSArIEludGVnZXIudmFsdWVPZihhdXgzKSk7ICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZXsKICAgICAgICAgICAgICAgICAgICBudW1lcm9zW2ldID0gYXV4OyAgICAgICAgICAgCiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgLy9SZWFsaXphIGEgc29tYSBkZSB0b2RvcyBvcyBlbGVtZW50b3MgZG8gYXJyYXkgZSBjYWxjdWxhIG8gZGlnaXRvIHZlcmlmaWNhZG9yCiAgICAgICAgICAgIC8vbmEgYmFzZSAxMCBkZSBhY29yZG8gY29tIGEgcmVncmEuICAgICAKICAgICAgICAgICAgZm9yKGludCBpID0gbnVtZXJvcy5sZW5ndGg7IGkgPiAwIDsgaS0tKXsKICAgICAgICAgICAgICAgIGlmKG51bWVyb3NbaS0xXSAhPSBudWxsKXsKICAgICAgICAgICAgICAgICAgICBzb21hICs9IEludGVnZXIudmFsdWVPZihudW1lcm9zW2ktMV0pOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHJlc3RvID0gc29tYSUxMDsKICAgICAgICAgICAgZHYgPSAxMCAtIHJlc3RvOwogICAgICAgICAgICAvL3JldG9ybmEgbyBkaWdpdG8gdmVyaWZpY2Fkb3IKCiAgICAgICAgICAgIHJldHVybiBkdjsKICAgICAgICB9IAogfQ==