0

i wrote a code of a game which gives you a word to unscrabble.But,not getting"You win", even after writing the correct word. All i get is" Wrong answer".Please find out the mistake and suggest ways to improve.

import java.util.*;
public class game {
public static void main(String args[]){
    Scanner input=new Scanner(System.in);
    Random rand=new Random();

    //THE WORD DIRECTORY
    String[][] list=new String[][]{
            {"google","a search engine"},
            {"facebook","a social networking site"},
            {"java", "this language"},
      };

    //CHOOSING THRE WORD
    int n=rand.nextInt(3);
    String theword=list[n][0];
    String thehint=list[n][1];

    //JUMBLING THE WORD
    String jumbledword=theword;
    char a[] = jumbledword.toCharArray();
    for(int i=0;i<jumbledword.length();i++){
        int input1=rand.nextInt(jumbledword.length());
        int input2=rand.nextInt(jumbledword.length());
        char temp=a[input1];
        a[input1]=a[input2];
        a[input2]=temp;

    }

    //THE GAME

        System.out.println("\t\tWELCOME TO JUMBLE WORD");
        System.out.println("1.Unscrabble the given word");
        System.out.println("2.Press 'hint'for hint");
        System.out.println("3.Press 'quit' to quit");
        System.out.print("The word:" );
        System.out.println(a);
     String guess;
    do{    guess=input.nextLine();

    if(guess==theword){
        System.out.println("YOU WIN");
    }
    else if(guess=="hint"){
        System.out.println(thehint);
    }
    else if(guess=="quit"){
        System.out.println("Better luck next time. The answer is :");
        System.out.println(theword);
    }
    else{
        System.out.println("Wrong answer .Try again");
    }




}while(!guess.equals("quit")&&!guess.equals(theword));

}}

Any suggestions are welcome.

1
  • 2
    Don't use == to compare Strings. Commented Mar 4, 2015 at 10:56

3 Answers 3

2

The == operator is used to check for object equality, not value. You want to use the equals method instead.

Instead of

if(guess==theword){

use

if(guess.equals(theword)){

Just to elaborate further, imagine you have three strings:

String s1 = new String("abc");
String s2 = s1;
String s3 = new String("abc");

In this case, the following results would occur:

s1.equals(s2); // true, same value
s1 == s2; // true, same object
s1.equals(s3); // true, same value
s1 == s3; // false, different objects
Sign up to request clarification or add additional context in comments.

Comments

2

Use the equals method of the String class to compare the contents of two Strings.

Using == on two class instances will just compare whether they are the same object.

For example:

if(guess.equals(theword))

Comments

1

Use equals instead of ==.

Change if statement as, if(guess.equals(theword)){

Explaination:

In Java, when the “==” operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory. In other words, it checks to see if the 2 object names are basically references to the same memory location.

equals method can be overridden to compare content of the objects. As is done in String class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.