Luke macLean
Luke macLean

Reputation: 3

Word Scramble game won't compare user guess to original word, Java

Hi everyone i am having trouble getting my program to compare the users guess to the original word that was read from the file. I know there are a lot of problems with this and its not overly pretty at the moment but any help would be greatly appreciated. basically the class userGuess should read a random word from the file "Words.txt" , then it should take the word it picks and scramble it in the ScrambleWord method, and display the scrambled word. next it should ask the user for their guess and compare that to the original word using the compareGuess method and tell the user if they are right or wrong.

here's my class

import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class userGuess 
{
private String guess;
private String word;
private String answer;
public userGuess()
{
    guess = "";
    word="";
    answer="";
}

public String getWord()
{
    try
    {
        Random r = new Random();
        File file = new File("Words.txt");
        Scanner inputFile = new Scanner(file);
        while (inputFile.hasNext())
        {
            word = inputFile.nextLine();
        } 
    inputFile.close();
    }
    catch (IOException IOError)
    {
        word="Error" + IOError;
    }
    return word;
}

//Scamble Word
public String ScrambleWord(Random rn, String sWord)
{
    char a[] = sWord.toCharArray();

    // Scramble the letters  
    for( int i=0 ; i<a.length-1 ; i++ )
    {
        int j = rn.nextInt(a.length-1);
        // Swap letters
        char temp = a[i]; a[i] = a[j];  a[j] = temp;
    }       

    return new String( a );   

}

public String userGuess()
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter Guess here: ");
    guess = keyboard.nextLine().toUpperCase();
    return guess;
}    

public String compareGuess()
{
    if ( guess.equalsIgnoreCase(word))
    {
        System.out.println("Winner");
    }
    else if(guess != word)
    {
        System.out.println("Try Again");
    }
    return "";
}
}

here's my main class

import java.io.*;
import java.util.Random;
import java.util.Scanner;
//import com.WordScramble.MacLean.ScrambleWord;
import com.WordScramble.MacLean.userGuess;
public class WordGame 

{   
public static void main(String[] args) throws IOException
{
// Declare Variables
    String word="";
    String wordCopy="";
    Random r = new Random();
    File file = new File("Words.txt");
    Scanner inputFile = new Scanner(file);
    word = inputFile.nextLine();
    wordCopy = inputFile.nextLine();
    //System.out.println(word);  

    //get scrambled word
    userGuess sWord = new userGuess();
    System.out.println(sWord.ScrambleWord(r, word));

    //get user guess
    userGuess uGuess = new userGuess();
    System.out.println(uGuess.userGuess());

    //compae user guess
     userGuess compare = new userGuess();
     System.out.println(compare.compareGuess());


}

}

the problem is when i enter the user guess it always tells me i'm right even if its wrong, again any help is greatly appreciated.

Upvotes: 0

Views: 211

Answers (1)

Java Doe
Java Doe

Reputation: 346

at first glace your code has some problems. Your program always accepts because your "comparing" is done using a new instance of userGuess. Calling

//compae user guess
userGuess compare = new userGuess();
System.out.println(compare.compareGuess());

creates a new instance of the class using your constructor

public userGuess()
{
    guess = "";
    word="";
    answer="";
}

hence all your variables contain an empty string. Comparing with your method

guess.equalsIgnoreCase(word)

results in

"".equalsIgnoreCase("")

which is always true.

You are also reading your user guess with a new instance of userGuess which results in saving your guess in another object than the one used to scramble your word or used to compare.

//get user guess
userGuess uGuess = new userGuess();
System.out.println(uGuess.userGuess());

Furthermore, you are reading your word in your program's main method an not saving it in any instance of userGuess, so none of your userGuess objects contain the input word.

You have to use the same object every time. So try something like

userGuess guess = new userGuess();
System.out.println(guess.scramble(r, guess.getWord()));
System.out.println(guess.userGuess());
System.out.println(guess.compareGuess());

Upvotes: 1

Related Questions