-1
Gentlemen, I am trying to do a routine that receives a certain text and a word from the user, after checking if within the text there is a word equal to what was requested. My attempt was to generate a counter that checks the character at the counter position and if it is not a blank space concatenates the character into a string. If it is a blank space, it checks whether the content of the string is equal to the text sought by the user. However my code is not returning a value, how do I correct.
Note: There may be new methods that perform the process without the need to use this process, but my intention is to learn how to work better with Strings, Characters and Loops.
Follows excerpt from the code.
// Faz a separação manual de palavras e verifica .equals
for(cont = 0; cont < texto.length(); cont++){
a = texto.charAt(cont);
if(!a.equals(" ")){
montagem += a;
} else {
if(montagem.equals(palavraBusca)){
palavras++;
} else {
montagem = "";
}
}
}
lblResultado.setText(Integer.toString(palavras));
ta giving error? returns 0 or wrong value?
– rLinhares
I was using a showMessageDialog to check what happens to each loop and instead of separating words and comparing with the typed one it is forming a sentence.
– Gabriel Galdino
That’s because you only "Zera" the
montagem
if I find it. I changed the answer, see if it works– rLinhares