If statement String trouble

Posted by Jeremy Stone on Stack Overflow See other posts from Stack Overflow or by Jeremy Stone
Published on 2013-10-30T01:21:14Z Indexed on 2013/10/30 3:54 UTC
Read the original article Hit count: 171

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about string