While problem in java (simple exercise)

Asked

Viewed 20 times

1

Context: I’m making an algorithm that stores random strings typed by the user in a string array until the user type "stop".

problem: when typing stop, the eclipse continues the loop in while, and in if (wordDigited != "stop"), even if the wordDigited == stop it keeps entering that if.

code:

while (palavraDigitada != "parar") {

        System.out.print("Escreva quaisquer string ou digite parar para buscar: ");         
        palavraDigitada = tec.next();
        if (palavraDigitada != "parar") {
            if (palavrasEmLista == "") {
                palavrasEmLista = palavraDigitada;
            }

            else {
                palavrasEmLista = palavrasEmLista + ";" + palavraDigitada;
            }               
            contador++;
        }
    }
  • Do not use == or != to compare Strings. Use the method equals. See the linked question to understand why.

No answers

Browser other questions tagged

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