Problemas com manipulação de Strings

6 respostas
V

Galera, preciso fazer o seguinte:

Tenho uma string STR, perciso pegar uma posição i dela e multiplicar por 1.

estou fazendo certo ?

for (i=0;i<str.length();i++){
	
		Mult = str.substring(i,i);
		soma2=Integer.parseInt(Mult)*1;

se vcs nao intenderem eu passo a funçao inteira ok :slight_smile: valeus

6 Respostas

R
mult = str.charAt(i);

Isso ?

V

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

mult = str.charAt(i);

vai retornar o caracter na posição i

tendeu?

R

pra transformar um char em uma string simplesmente some com “”.

String tntoFaz = str.indexOf(i) + "";
V

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

}

}

Criado 8 de agosto de 2005
Ultima resposta 8 de ago. de 2005
Respostas 6
Participantes 3