CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    Nov 2011
    Posts
    0

    Unhappy Hangman Java Code Error

    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:



    import java.util.*;
    import java.util.Scanner;
    /** 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 = "****";
    String inputString;
    char guess;
    int position = 0;
    // Prints welcome message and displays the asterisks.
    System.out.println("Welcome to Hangman!");
    System.out.println(displayPhrase);
    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!");
    else
    displayPhrase = displayPhrase.substring(0,position) +
    guess +
    displayPhrase.substring(position+1,displayPhrase.length());
    System.out.println(displayPhrase);
    {
    // Displays a winning or losing message.
    --------->> if (guess.equalsignorecase( targetPhrase )) <<-----------
    System.out.print(" You won! The word is " + targetPhrase);
    else
    System.out.print("You lose! Muahahaha");

    }
    }
    }
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Hangman Java Code Error

    'guess' is a primitive of type char. You can't call methods on a primitive type - they are not objects.

    You need to either convert the char to a String or convert the String to a char (which you can only do if the String contains exactly one character).
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Hangman Java Code Error

    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

  4. #4
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: Hangman Java Code Error

    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured