Results 1 to 5 of 5
  1. #1
    jmcquaid1987 is offline Member
    Join Date
    Mar 2014
    Posts
    12
    Rep Power
    0

    Default 'Unexpected type', a returned variable required, please help.

    I'm making a Hangman program for Java, here's my code:

    import java.util.Scanner;

    public class Hangman {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    String answerWord = WordList.getWord();
    Scanner keyboard = new Scanner(System.in);

    StringBuilder displayWord;


    for (int i=1; i <= answerWord.length(); i++) {
    displayWord.append("*");
    }
    while (displayWord.toString().contains("*")) {
    System.out.println(displayWord);
    System.out.print("Enter a letter: ");

    String userInput=keyboard.nextLine();
    char toCheck=userInput.charAt(0);

    for (int i; i <= answerWord.length(); i++) {
    if (toCheck == answerWord.charAt(i)) {
    displayWord.charAt(i)=userInput.charAt(0);
    }
    }
    }



    System.out.println("The word was... " + answerWord);
    System.out.println("You win!");
    }
    }

    The Compiler gives me this only error:

    Hangman.java:39: error: unexpected type
    displayWord.charAt(i)=userInput.charAt(0);
    ^
    required: variable
    found: value

    I understand that charAt returns i as a value and not a variable. What do I use to return a variable as I intended?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: 'Unexpected type', a returned variable required, please help.

    Read the API documentation for the StringBuilder class; it has a method that can set a single char value in the builder.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jmcquaid1987 is offline Member
    Join Date
    Mar 2014
    Posts
    12
    Rep Power
    0

    Default Re: 'Unexpected type', a returned variable required, please help.

    I know, I'm just beyond frustrated because I have no idea where to look for it or how to find it. The if statement is supposed to see if the variable toCheck is the same as the character at location i
    and then set the character at the same location in displayWord to be c, or b, or a or whatever the letter is.
    Last edited by jmcquaid1987; 04-02-2014 at 06:06 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: 'Unexpected type', a returned variable required, please help.

    Here's a link to the complete (version 7) API documentation: Java Platform SE 7

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    jmcquaid1987 is offline Member
    Join Date
    Mar 2014
    Posts
    12
    Rep Power
    0

    Default Re: 'Unexpected type', a returned variable required, please help.

    Thank you, I see the list, but I don't know which one I should use.

Similar Threads

  1. Replies: 8
    Last Post: 10-28-2012, 04:47 AM
  2. Problem with Unexpected Type Error
    By Emo in forum New To Java
    Replies: 2
    Last Post: 10-05-2011, 08:45 AM
  3. Unexpected text returned from getPath()
    By madroadbiker in forum New To Java
    Replies: 3
    Last Post: 04-23-2011, 04:51 PM
  4. Error: unexpected type
    By silvia in forum New To Java
    Replies: 3
    Last Post: 02-05-2010, 09:54 PM
  5. unexpected type
    By one in forum New To Java
    Replies: 13
    Last Post: 01-20-2009, 09:32 AM

Posting Permissions

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