SkyVar
SkyVar

Reputation: 351

How to pass a boolean and verify a boolen in an if statement?

OK I'm on the final part of my game, then I will invite you all to play. Its not perfect by no means, but its a huge milestone in my Java progress. So in my code I'm calling my puzzle class to verify if the solution given by the user equalsIgnoreCase of the private variable solution provided in the puzzle class. If they match it returns true, else, it returns false. On the tester, if I get the true value returned, it should print I was right and if it returns false I get it wrong. The problem is that I type it in right, but it tells me I'm wrong. Why?

puzzle:

    public boolean solvePuzzle(String answer)
    {
        if(this.solution.equalsIgnoreCase(answer))
        {
            return true;
        }
        else
        {
            return false;

        }

    }

}

puzzletester:

    if(choice==2)
   {
        System.out.println("Please solve the puzzle");
        input.nextLine();
        String answer=input.next();
        answer=answer.toUpperCase();
        game.solvePuzzle(answer);

        if(game.solvePuzzle(answer)==true)
        {
        System.out.println("That is correct");
        }
        else
        {
        System.out.println("You are wrong");
        }
        }


        }

Upvotes: 0

Views: 154

Answers (2)

DSJ
DSJ

Reputation: 106

Your problem is actually just using Scanner, which by default deliminates using whitespace. You need to call the useDelimiter() method to set it up correctly. You could use a BufferedReader:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in))

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691715

OK. Hint to the solution: Read carefully the javadoc of the Scanner class, and especially the part explaining what the default delimiter is, as well as the documentation of the next() method. Now look at what the solution is, and ask yourself if it could have a relationship with what the default delimiter is.

To confirm, choose another solution and see if it changes something. Also print the values of the variables you compare to see what they actually contain, or learn to use the Eclipse debugger. It's not so hard, and I'm sure Eclipse has a help page about it.

Upvotes: 1

Related Questions