Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2007
    Rep Power

    Default Cannot convert from char to String error

    Still working on my hangman program from yesterday (Thanks Shoeninja for help!)

    I re-did a bunch of it to include global variables and such

    here is the method I am still having problems with:
    BuildWord is a global string variable with the value " " (8 spaces). the loop compares your 1-char guess with all 8 characters of a randomly chosen 8-letter word. What buildword does is keep track of the word you've guessed so far. So say the word is aardvark, and you guess "a" I want buildword set to "aa a ", using cnt from the for loop to replace the appropriate spaces.

    "String tempword = BuildWord.charAt(cnt)" is where I am getting the cannot convert from char to string error.

    Java Code:
    public static void compare ()
    BuildWord="        ";
    for (int cnt=0;cnt <=7; cnt++)
    {//begin for
    	String templetter = guessedletter;
    	String tempword = BuildWord.charAt(cnt)
    	//compares your one character guess to every character of the wordchosen
            if (guessedletter.charAt(0) == wordchosen.charAt(cnt))
             tempword = templetter;
    }//end for


  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Rep Power


    Java Code:
    String tempword = BuildWord.charAt(cnt)
    You are trying to assign a character to a string variable. (And you seem to forget ; at the end of the statement.)

    Try this:

    Java Code:
    String tempword = (new Character(BuildWord.charAt(cnt))).toString();

Similar Threads

  1. How to convert a string into a BigInteger
    By valery in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 01:32 PM
  2. How can i insert a char into a string
    By Jamie in forum New To Java
    Replies: 8
    Last Post: 02-17-2011, 08:59 PM
  3. Char to String in java
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 01:42 PM
  4. Help with, String, Char
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 02:58 PM
  5. Error: convert from String to long
    By bbq in forum New To Java
    Replies: 1
    Last Post: 06-29-2007, 07:23 PM

Posting Permissions

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