Take the 2-minute tour ×

I have looked online and all of the tutorials / questions have pointed me to this. I can't see why this isn't working. Any help would be much appreciated. Thanks

import java.util.*;

public class test {
    static Scanner userInput = new Scanner(System.in);
    public static void main(String[] args) {
        String textEntered = userInput.next();
        if (textEntered == "hello") {
            System.out.println("Hello to you too!");

I enter "hello" but nothing is printed. Also I have tried next() and nextLine();

share|improve this question
I find it truly astonishing if you've looked at any significant Java tutorials and they haven't talked about Strings and == vs. equals. :-) –  T.J. Crowder Oct 13 '12 at 9:36
A quick search of SO gives us these questions and their answers: Java String Comparison, Java string comparison?, How do I compare strings in Java?... –  T.J. Crowder Oct 13 '12 at 9:39
I do not see the while loop in the snippet though ... did u want to go on a loop for user input ? –  angel_007 Oct 13 '12 at 9:40
I have been watching some online youtube tutorial and he had not mentioned that and when I looked online, most used very similar code and I didn't realise that it was the if statement that wasn't working. Also, in the real code it was in while loop and I just simplified it which is why i tagged loops by mistake. –  Mikeecb Oct 13 '12 at 9:48

3 Answers 3

up vote 6 down vote accepted

A couple of things:

  1. You've said "while loop" in your question title, but there's no while loop in your code. So it will only check the first token you type, not subsequent tokens.

  2. In Java, you don't use == to compare strings, you use the equals method (or sometimes equalsIgnoreCase).


    if (textEntered == "hello") {


    if (textEntered.equals("hello")) {

    The == operator, when used with object instances (and String instances are objects) checks to see if the two operands point to the same object, and so it's not true if you use it to compare two different String objects that have the same sequence of characters.

share|improve this answer
that works! thanks a lot :) I used to c++ and just learning java and have to get used to a few things! Thanks a lot though! –  Mikeecb Oct 13 '12 at 9:46

In java you can not compare string by == operator , You need to use stringOne.equals(stringTwo).

== will compare ObjectLocation where as .equals() compares actual string which is provided by String Class in java

share|improve this answer

The reason it may not be working is because you aren't comparing the actual string values.


Try using something like.

 if (textEntered.compareTo("hello") > 0) {
    System.out.println("Hello to you too!");

That might help you out.

share|improve this answer
That link that you pointed out says: "use the .compareTo() method to test for unequal comparisons". .compareTo returns 0 for equal objects. –  Costi Ciudatu Oct 13 '12 at 13:07
Well you use that to test if the two strings are the same. If they are it will return a 1. If not then a negative 1. But either way both functions will give you the exact same thing that you want. The compareTo function can be used for not only unequal but equal as well. So it's better to know that then just hte equal function. And what I was getting at mostly, was that Mikeecb's problem was an easy google search away. Hence why I said it might help you out. –  Dean Marsden Oct 13 '12 at 15:05
Have a look at the Javadoc for Comparable.compareTo() to see what it's supposed to return. –  Costi Ciudatu Oct 13 '12 at 18:24
compareTo int compareTo(T o) Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. –  Dean Marsden Oct 14 '12 at 9:23

Your Answer


