0

I'm trying to ask the user to enter a character ("y"/"n") and check whether or not that was the right answer. I'm getting the following error: "incomparable types: java.util.Scanner and java.lang.String"

Scanner userInput = new Scanner(System.in);

System.out.printf("Is this word spelled correctly?: %s", wordToCheck);
        rightCheck(userInput);

public boolean rightCheck(Scanner usersAnswer)
{
    if(usersAnswer == "y")
    {
        //"Correct!"
        //Increment User's Score
    }
    else
    {
        //"Incorrect"
        //Decrement User's Score
    }
}
Siva Arunachalam
  • 7,582
  • 15
  • 79
  • 132
FiringBlanks
  • 1,998
  • 4
  • 32
  • 48

3 Answers3

4

Yes, because a scanner is a way of getting input rather than the value itself. You want to fetch the next value from the input, and then compare it. Something like this:

String answer = scanner.next();
if (answer.equals("y")) {
    ...
} else if (answer.equals("n")) {
    ...
}

Note that you should usually (including this case) not compare strings with ==, as that compares whether the two operands refer to the exact same string object - you're only interested in whether they refer to equal objects. (See this question for more details.)

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • you could make the program less error-prone by switching the arguments of `equals`: `"y".equals(answer)` This will not throw `NullPointerException` if `answer` is `null` – El Hocko Feb 22 '13 at 07:15
  • @cIph3r: `Scanner.next()` never returns null, so there's no need to do that here. If it ever *did* return null, that would indicate that something's very broken - so I'd rather know about it via an exception than continue in a world of brokenness. – Jon Skeet Feb 22 '13 at 07:18
  • ok, right. I the case of `scanner.next()`, there is no null-value. But in general, this prevents such errors. – El Hocko Feb 22 '13 at 07:24
  • @cIph3r: My point is that something it *prevents* errors - but in other cases it *propagates* them. You should always know whether or not you're expecting that a string can be null - and decide what to do based on that assumption. If the assumption is wrong, I'd rather know about it sooner rather than later. – Jon Skeet Feb 22 '13 at 07:32
0

I believe you should first get the String from Scanner (through next() maybe?). Then in your method, do not use "==" as a string comparator.

Ian
  • 691
  • 3
  • 9
0

I have modified your code, haven't tested it but it should work:

 Scanner userInput = new Scanner(System.in);

System.out.println("Is this word spelled correctly?:" + wordToCheck);
        rightCheck(userInput.next());//send the string rather than the scanner

public boolean rightCheck(String usersAnswer)//convert the parameter type to String
{
    if(usersAnswer == "y")
    {
        //"Correct!"
        //Increment User's Score
    }
    else
    {
        //"Incorrect"
        //Decrement User's Score
    }
}
Aashray
  • 2,753
  • 16
  • 22