0

Possible Duplicate:
How do I compare strings in Java?

I'm working on a small program that asks for your name using a Scanner. If you enter blankstring, then I would like the console to display a message.

Here's what I tried doing:

import java.util.Scanner; 
public class Adventure
{  
    public static void main(String[] args) 
    {      
        Scanner myScan = new Scanner (System.in);                             
        System.out.println("What's your name?");                                 
        String name = myScan.nextLine();

        while (!(name == ""))   //Always returns false.
        {
            System.out.println("That's not your name. Please try again.");
            name = myScan.nextLine();
        }

        System.out.println("It's a pleasure to meet you, " + name + ".");
    }
}

The code never enters the while loop. Why?

Community
  • 1
  • 1
  • 1
    Welcome to StackOverflow. Please be sure to provide a description of the problem you are having so others don't have to guess. – Reinstate Monica -- notmaynard Feb 04 '13 at 21:36
  • -1 (This can be undone) Please write a useful title and describe the problem. In this case, look up `[java] string not equal` in the SO search for *many, many* duplicate questions. –  Feb 04 '13 at 21:37
  • http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java/513839#513839 –  Feb 04 '13 at 21:38

1 Answers1

13

Change your condition to:

while(!name.equals("")) {

or as suggested below by m0skit0:

while(!name.isEmpty()) {

See also

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674