-1

I'm making a vocabulary practice program using arrays (I know, not Lists, but I need array practice). However, everything goes well until the program evaluates if the typed answer (when the program quizzes the user) is the actual definition. I always get the answer wrong, and I don't know why. Here's the whole program:

import java.util.Scanner;

public class organ {
public Scanner input = new Scanner(System.in);
public String language;
public int temp;

public static void main(String[] args){
    organ organObject = new organ();
    organObject.greeting();
    organObject.ending();
}

public void greeting(){
    System.out.println("Hello! Do you want to practice vocabulary in English or Spanish?");
    //Need to fix this grammar.
    System.out.println("!Hola! ?Quieres practicar el vocabulario en ingles o espanol?");
    language = input.nextLine();
    checkLanguage(language);
}
public void checkLanguage(String answer){
    if (language.equals("English") || language.equals("ingles")){
        englishTree();
    }
    else{
        spanishTree();
    }
}
public void englishTree(){
    System.out.println("How many words do you want to practice using?");
    int temp = input.nextInt();
    String[] wordStorage = new String[temp];
    String[] meaningStorage = new String[temp];
    String answer;
        for(int counter = 0; counter < wordStorage.length; counter++){

            System.out.println("What is word number " + (1 + counter) +   "?");
            wordStorage[counter] = input.next();

            System.out.println("What is def number " + (1 + counter) + "?");
            meaningStorage[counter] = input.next();
        }
    if (wordStorage.length > 10) {
        System.out.println("Stop. Now.");
    }
    System.out.println("Alright, let's get to the good stuff.");
    for(int counter = 0; counter < wordStorage.length; counter++){
        System.out.println("What is the meaning of " + wordStorage[counter] + "?");
        answer = input.next();
        if (answer == meaningStorage[counter]){
            System.out.println("Correct!");
        }
        if (answer != meaningStorage[counter]){
            System.out.println("Wrong. The answer is " + meaningStorage[counter] + ".");
        }
    }
}
public void spanishTree(){
    System.out.println("Espere.");
}
public void ending(){
    System.out.println("This is the ending of the program for now. Bye!");
}

} The problem is under "Alright, let's get to the good stuff."

  • Thanks! It's funny, I actually had another post very similar to this and still didn't remember this. I appreciate the help! – user3538387 Jun 07 '14 at 02:50

1 Answers1

1

You're using == to compare Strings. Use .equals()

str1.equals(str2) instead of str1 == str2.

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75