Results 1 to 6 of 6
  1. #1
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default Incompatible types

    Hello,
    i was making a hangman game because i thought this was fun and good for practicing with java.

    Now i started, but soon im getting an error

    it is this error
    Incompatbile types
    Required: char
    Found: Java.lang.string
    i tried googlin' it but it seems to be an error that depens on my code.

    so here is my code:
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package javaapplication5;
    
    /**
     *
     * @author Jesse Tielens
     */
    import java.util.Scanner;
    public class Main {
    
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
           System.out.println("Payer 1, please choose a word that player has to guess");
           Scanner input = new Scanner(System.in);// making an input scanner to get the word
           String str = input.nextLine();
           System.out.println("Player 1 has chosen a word; Player 2. You can now tyr to guess it");
           str.toCharArray();// convert the String to an array(i think i need this later to guess the words)
           Scanner guess = new Scanner(System.in);// make a new scanner for player 2 to enter characters
           char letter = guess.nextLine();// here is error!!
        }
    
    }
    Anybody know what i'm doing wrong?
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

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

    Default

    Read the API documentation for the Scanner.nextLine() method: it returns a String and you are trying to assign that to a variable of type char; that was what your compiler was trying to tell you.

    kind regards,

    Jos

  3. #3
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    ok now i understand, i checked the API but can't find anything that gets input for a char you know one?
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

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

    Default

    Quote Originally Posted by coltragon View Post
    ok now i understand, i checked the API but can't find anything that gets input for a char you know one?
    Readers can read single characters but beware: standard input is line buffered so you always need to type <enter> to stop the Reader from reading from that character stream. Scanners are far too 'sofisticated' to read single characters. Read the API documentation for the InputStreamReader; it converts simple byte oriented InputStreams to character oriented Readers. You can wrap that basic Reader in a BufferedReader if you want.

    kind regards,

    Jos

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default

    Just take in a String and check that it is only 1 character long (String has a length property).

  6. #6
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    wow thanks, you're really helpful.
    Beginner in Java Programming, Please don't trust my anwsers blind please :D

Similar Threads

  1. incompatible types...What is "Dispatch" ?
    By ollyworks in forum Java Applets
    Replies: 1
    Last Post: 09-10-2009, 04:03 AM
  2. Replies: 7
    Last Post: 08-28-2009, 09:23 AM
  3. Replies: 3
    Last Post: 12-14-2008, 02:52 PM
  4. Replies: 6
    Last Post: 10-08-2008, 09:09 AM
  5. problem with scanner class:incompatible types
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 08:02 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
  •