Hey, I'm having a problem comparing a char to a String. The error is: char cannot be dereferenced. Any suggestions? I've put arrows around the line that holds the error.
Here's my code:
/** Created by Shakiya Samantha Charles. This code will play one game of very simple hangman.
public class Guess
public static void main(String args) throws Exception
Scanner console = new Scanner(System.in);
// Strings the word game for the fixed game.
String targetPhrase = "game";
String displayPhrase = "****";
int position = 0;
// Prints welcome message and displays the asterisks.
System.out.println("Welcome to Hangman!");
while (displayPhrase.indexOf('*') != -1)
// Allows use to guess a letter.
System.out.println("Guess a Letter");
inputString = console.next();
guess = inputString.charAt(0);
position = targetPhrase.indexOf(guess, position+1);
if (position == -1)
System.out.println("Sorry - guess again!");
displayPhrase = displayPhrase.substring(0,position) +
// Displays a winning or losing message.
--------->> if (guess.equalsignorecase( targetPhrase )) <<-----------
System.out.print(" You won! The word is " + targetPhrase);
System.out.print("You lose! Muahahaha");
it is easier to convert a character to a string one of the constructors in the string class takes the argument an array of characters. You can then use the equals method from the string class to compare two strings
one of the most common mistake I've seen people do in Java is compare one string with another with an == sign this is the wrong approach as all variables in Java I mean the class variables are in essence pointers so what you're doing is comparing one pointer with another pointer please be mindful of this