vinnymaran
nao , Mult nao eh um Char eh uma String
Eu tenho uma funçao em Delphi disso , estou tentando converter para java:
function Codigo_Barra(Str: String): String;
Var DAC,i,Mult,Soma; Integer;
begin
Soma := 0;
for i :=1 to Length (Str) do
begin
if ( i mod 2) = 0 then Mult:= (StrToInt(Copy(Str,i,1))) * 1
else Mult:= (StrToInt(Copy(Str,i,1))) * 2;
if ( Mult >= 10 ) then
Mult := StrToInt(copy(IntToStr(Mult),1,1)) +
StrToInt(copy ( IntToStr(Mult),2,1));
Soma:=Soma+Mult;
end;
Soma:=(Soma Mod 10);
if Soma<> 0 then
DAC:= 10 -soma
else DAC:=0
end
marcelo_mococa
cara, mas pra q vc multiplicar por 1…
o valor vai ser o mesmo…
1*1 = 1
1*2 = 2
1*3 = 3
1*456 = 456
marcelo_mococa
“charAt” retorna um caractere especifico de uma string
vai retornar o caracter na posição i
tendeu?
vinnymaran
consigui galera o problema tava em outro lugar
tava assim: if (1%2 == 0)
tem q ser assim : if ((i+1)%2 == 0)
o resto ta certo ai vai o meu gerador de digito verificador :
private String calculaDAC(String str) {
int DAC = 0, i, Soma, Mult = 0;
Soma = 0;
try {
for (i = 0; i < str.length(); i++)
{
if ((i+1)%2 == 0)
{
Mult = Integer.parseInt(str.substring(i, i + 1)) * 1;
}
else
{
Mult = Integer.parseInt(str.substring(i, i + 1)) * 2;
}
if (Mult >= 10)
{
Mult = Integer.parseInt(Integer.toString(Mult).substring(0,1)) +
Integer.parseInt(Integer.toString(Mult).substring(1,2));
}
Soma = Soma + Mult;
}
Soma = Soma % 10;
if (Soma != 0)
{
DAC = 10 - Soma;
}
else
{
DAC = 0;
}
} catch (Exception e) {
e.printStackTrace();
}
return Integer.toString(DAC);
}
}