If it’s not working properly

Asked

Viewed 23 times

0

Hello, guys. All right? So I’d like to ask you a question. I created the following code:

    import java.util.Scanner;

public class Main
{
    public static void main(String[] args) {
        Scanner entrada = new Scanner(System.in);
        
        String Resposta = "";
        System.out.println("Digite a Combinação correta");
         System.out.println("L, M, N, B");
         System.out.println("Q, S, R, P");
         System.out.println("D, G, H, V");
         Resposta = entrada.nextLine();
         System.out.println(Resposta);
                 if(Resposta == "Floripa"){
            System.out.println("Show! Sou de Floripa");
        }
 

    }
}

the problem is this, when I type Floripa, the variable changes the value to Floripa, however if does not work

1 answer

0

To compare Strings in Java, it is necessary to use the function equals, and not the operator ==. To work, do:

import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner entrada = new Scanner(System.in);

    String Resposta = "";
    System.out.println("Digite a Combinação correta");
     System.out.println("L, M, N, B");
     System.out.println("Q, S, R, P");
     System.out.println("D, G, H, V");
     Resposta = entrada.nextLine();
     System.out.println(Resposta);
             if(Resposta.equals("Floripa")){
        System.out.println("Show! Sou de Floripa");
    }


}
}

Browser other questions tagged

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