Problem with application [duplicate]

-3

I'm trying to make a program to ask French words to myself. I've done this code, but whatever I put in, "Your answer is incorrect." (else condition). Help please.

import java.util.Scanner;
public class AprenderPalabras {

    public static void main(String[] args) {
        Scanner wordL= new Scanner(System.in);
        Scanner wordR = new Scanner(System.in);
        Scanner answer= new Scanner(System.in);

        String wordL1= wordL.nextLine();
        String wordR1= wordR.nextLine();

        System.out.println(wordL1 + " -> ");

        String answer1= answer.nextLine();
        if (answer1==wordR1){
            System.out.println("Your answer is correct.");
        }
        else {
            System.out.println("Your answer is incorrect.");
        }
    }
}
    
asked by JuanVan12 23.09.2016 в 12:34
source

1 answer

3

In java the operator == check that both variables contain the same instance, to compare if two text strings continue the same text you have to use the "equals" method, in your case it would be:

answer1.equals(wordR1)

More examples would be:

// Estas dos cadenas tendrian el mismo valor
new String("prueba").equals("prueba") // --> true 

// Estas dos variables no tienen la misma instancia
new String("prueba") == "prueba" // --> false 

// tampoco estas dos
new String("prueba") == new String("prueba") // --> false 

// las cadenas literales las maneja internamente el compilador
// por lo que si tienen la misma cadena comparten instancia
"prueba" == "prueba" // --> true 
    
answered by 23.09.2016 в 12:41