If statement String trouble
- by Jeremy Stone
I'm trying to create a program which takes user input of two words and determines whether or not these words are the same.
import java.util.Scanner;
public class L7E3 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System. in );
String word1, word2;
System.out.println("Please enter a word: ");
word1 = keyboard.nextLine();
System.out.println("Please enter a word: ");
word2 = keyboard.nextLine();
if (word1 == word2) {
System.out.println("The words are " + word1 + " and " + word2 + ". These words are the same.");
} else {
System.out.println("The words are " + word1 + " and " + word2 + ". These words are not the same.");
}
}
}
I figured that word1==word2 would have worked to determine whether the two strings were equal, I'm using JGrasp and it goes directly to my else option regardless of input. Am I doing something wrong with strings?