-4

Here is my code (question listed below): import java.util.Scanner;

public class ExampleCode {

/**
 * B. Stephens
 */
public static void main(String[] args) {

    Scanner input = new Scanner (System.in);

    String secretPhrase = "show me the money";
    boolean notDone = true;

    while (notDone = true){
        System.out.print("Guess the puzzle: ");
        String puzzleGuess = input.nextLine();
        if (puzzleGuess == secretPhrase) {
            System.out.println("Congratulations! You guessed the puzzle!");
            notDone = false;
        }
    }

} // end main

For some reason, the program isn't recognizing when my input (puzzleGuess) is the same as secretPhrase. It seems like there is no reason the correct input shouldn't end the program. Thanks for any help!

5 Answers5

1

Use .equals() instead of == so, your if-statement should be like this: if (puzzleGuess.equals(secretPhrase))

as the String is considered to be an object, and any objects comparison must be performed using .equals(Object o).

Hope my answer did help :)

Khaled Hassan
  • 156
  • 1
  • 8
0

because you need to use the .equals method

if (puzzleGuess.equals(secretPhrase)) {
    System.out.println("Congratulations! You guessed the puzzle!");
    notDone = false;
}

Java Strings are interned so == can sometimes work.

public class InternedDemo {

    public static void main(String[] args) {
        String s1 = "hello world";
        String s2 = "hello world";
        String s3 = new String("hello world");

        System.out.println(s1 == s2); // true
        System.out.println(s1 == s3); // false
        System.out.println(s2 == s3); // false

       System.out.println(s1.equals(s3)); // true
       System.out.println(s2.equals(s3)); // true
   }
}
robbmj
  • 16,085
  • 8
  • 38
  • 63
0

Don't compare strings using ==. Use secretPhrase.equals(puzzleGuess). == is just checking if both strings are the same object.

splrs
  • 2,424
  • 2
  • 19
  • 29
0

Use .equals() to compare to string

public static void main(String[] args) {

Scanner input = new Scanner (System.in);

String secretPhrase = "show me the money";
boolean notDone = true;

while (notDone){
    System.out.print("Guess the puzzle: ");
    String puzzleGuess = input.nextLine();
    if (puzzleGuess.equals(secretPhrase)) {
        System.out.println("Congratulations! You guessed the puzzle!");
    secretPhrase=null;
        notDone = false;
    }
   }
}
Hammad
  • 101
  • 1
  • 10
0

== cannot be used to compare Strings. For non-primitives, == determines whether they are the same object. To see whether the two strings have the same value, instead use .equals():

String str1 = new String("foo");
String str2 = str1;
String str3 = new String("foo");
String str4 = new String("bar");
System.out.println(str3 == str3); //true
System.out.println(str1 == str2); //true
System.out.println(str1 == str3); //FALSE!
System.out.println(str1.equals(str3)); //true!
System.out.println(str1 == str4); //false
System.out.println(str1.equals(str4)); //false
Vitruvie
  • 2,327
  • 18
  • 25
  • 3
    == can be used to compare Strings, or any other Object for that matter. It is reference equality vs actual equality. – Sinkingpoint Dec 15 '13 at 23:20