Results 1 to 6 of 6
Thread: Incompatible types
- 01-15-2010, 04:11 PM #1
Senior Member
- Join Date
- Dec 2009
- Posts
- 104
- Rep Power
- 0
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 errori tried googlin' it but it seems to be an error that depens on my code.Incompatbile types
Required: char
Found: Java.lang.string
so here is my code:
Anybody know what i'm doing wrong?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!! } }Beginner in Java Programming, Please don't trust my anwsers blind please :D
- 01-15-2010, 04:18 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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
- 01-15-2010, 04:37 PM #3
Senior Member
- Join Date
- Dec 2009
- Posts
- 104
- Rep Power
- 0
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
- 01-15-2010, 04:46 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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
- 01-15-2010, 04:47 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Just take in a String and check that it is only 1 character long (String has a length property).
- 01-15-2010, 04:47 PM #6
Senior Member
- Join Date
- Dec 2009
- Posts
- 104
- Rep Power
- 0
Similar Threads
-
incompatible types...What is "Dispatch" ?
By ollyworks in forum Java AppletsReplies: 1Last Post: 09-10-2009, 03:03 AM -
Code errors (incompatible types, cannot find variables, etc)
By Menre in forum Advanced JavaReplies: 7Last Post: 08-28-2009, 08:23 AM -
Incompatible types??? Counting through an array of Strings
By ookie833 in forum New To JavaReplies: 3Last Post: 12-14-2008, 01:52 PM -
Error: incompatible types, found: int required: boolean
By silvia in forum New To JavaReplies: 6Last Post: 10-08-2008, 08:09 AM -
problem with scanner class:incompatible types
By fred in forum New To JavaReplies: 1Last Post: 07-20-2007, 07:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks