I've been recently working on this program, it's supposed to be able to confirm whether or not the guess you input is the same as any of the three answers. The issue is that no matter what I put, it always tells me the guess was wrong. I've checked the output in various places for consistency in the variables, but they all look the way they should. If you could tell me what went wrong, and how to fix it, that'd be much appreciated. Thanks!
import javax.swing.JOptionPane;
import java.util.Scanner;
import java.io.IOException;
import java.io.File;
import static java.lang.System.out;
public class TestDialogBox {
public static void main(String args[])
throws IOException{
Scanner riddleBook = new Scanner(new File("RiddleBook.txt")); //Initialize scanner for riddles & answers
String guess = null;
String answer1 = riddleBook.nextLine();
String answer2 = riddleBook.nextLine();
String answer3 = riddleBook.nextLine();
guess = JOptionPane.showInputDialog(riddleBook.nextLine()) ;
if (guess == answer1 || guess == answer2 || guess == answer3) {
JOptionPane.showMessageDialog(null, "Awesome!");
} else {
JOptionPane.showMessageDialog(null, "Bummer. The correct answer could have been: " + answer1 + ", " + answer2 + ", or " + answer3);
JOptionPane.showMessageDialog(null, "Your guess was: " + guess);
//JOptionPane results are for demonstrating the answers and your guess didn't change
}
}
}
The text file that it scans is as follows:
man
humans
people
What goes on four legs in the morning, two legs in the afternoon, and three legs in the evening?