Manual String Breaking in JAVA

Asked

Viewed 40 times

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

Layout

  • ta giving error? returns 0 or wrong value?

  • 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.

  • That’s because you only "Zera" the montagem if I find it. I changed the answer, see if it works

2 answers

0

I believe the problem may be about method equals(); this method compares the whole object, not just the value, as is its idea (for example, "home" would equal "home").

You could overwrite the method equals(), but that’s kind of risky since another part of the code might need the original. Therefore, I believe that the most viable comparison would be made with ==:

//inicialização de variáveis
montagem = "";
palavras = 0;

for(cont = 0; cont < texto.length(); cont++){
    a = texto.charAt(cont);
    
    if(!a.equals(" ")){
        montagem += a;
    } else {
        if(montagem == palavraBusca){
            palavras++;
        }

        //montagem deve ser reiniciada independente de ter sido encontrada
        montagem = "";
    }
}

//validação da última palavra do texto
if(montagem.equals(palavraBusca)){
    palavras++;
}

lblResultado.setText(Integer.toString(palavras));

detail: I am not with java compiler, the answer admits that your code is correct.

  • Actually if it’s a blank space I do the checking before adding, so when checking that I’m in a blank space the code should break regardless of whether it’s the word or not, in your code, so it turns out that it is a blank space the concatenation is already made. It took time but I was able to find the solution.

  • @Gabrielgaldino.. boy, I honestly didn’t see any difference in that code with what you wrote and marked as an answer, but follow the ball.. =)

-1


The logic for treatment is: If the counter reads a blank space it has two alternatives, it is either the word searched or not. So after checking the two will return the mounting variable to zero. If it is not a blank space the word has not yet finished. Follows excerpt from the code.

    for(cont = 0; cont < texto.length(); cont++){  
    a = texto.charAt(cont);
    if(Character.toString(a).equals(" ")){
        // verifica
        if(montagem == palavraBusca){
            palavras++;
        } 
        montagem = "";
    } else {
        montagem+= a;
    }
    if(montagem.equals(palavraBusca)) palavras++;
    }
    lblResultado.setText(Integer.toString(palavras));
  • Boy, I honestly didn’t see any difference in that code with what you wrote and marked as an answer, but follow the ball.. =)

  • @rLinhares Yeah, stopping to see really follows the same logic but I tried to use your suggestion and was not breaking the words.

  • @rLinhares The only difference with my code is that the last check that is the one that actually adds 1 in the amount of words this out of the loop is, so it will not fulfill its purpose. If I had it before I finished the loop I’d be right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.