I'm trying to make my first program where if the person inputs the right name (my name :)) they will win the game, but if they don't it will print something else. So in my if then statement which is displayed here:

if (yes == nicole) {
System.out.print("You guessed it! What a great name.");
}

it refuses to work. I know that when I did my calculator and did if (apples == 10) it worked but here it refuses to work correctly. Can you guys help? Here's the complete code:

You don't compare the content of two Strings with the == operator; use the equals( ... ) method instead; you can only compare primitives (int, long, double, char etc) for equality with the == operator.

kind regards,

Jos

09-19-2010, 02:42 PM

ComicStix

Can you explain further about this equal(...) method? I'm confused what you mean.

09-19-2010, 03:04 PM

ComicStix

O.K I changed my name to this:

if (yes.equals("nicole")) {
System.out.print("You guessed it! What a great name.");
}

but even when i print in nicole for the input it says i guessed the wrong name when I really did guess the right name.

09-19-2010, 03:09 PM

Fubarable

Next, you will want to read up on how to use a java.util.Scanner object. For one, calling next() gets the next token available and returns the String. My guess is that you want to use nextLine(), not next(), and nextLine also returns a String. but again, you'll want to create a separate String variable to store the String returned:

Code:

String response = yes.nextLine();
// then use the response String to test if it equals your key String.

09-19-2010, 03:15 PM

JosAH

Quote:

Originally Posted by ComicStix

Can you explain further about this equal(...) method? I'm confused what you mean.

You should bookmark (or even better download it) Java SE 6 Documentation and read all about all classes and all methods that are at your disposal.