JAVA - Converting char to CharSequence

Ludic

2[H]4U
Joined
Jun 10, 2004
Messages
2,218
I'm attempting to use this new method in 1.5.0 for a Hangman program for my programming class
Code:
public static boolean compareTo(CharSequence z)
{
	return(guessThis.contains(z));
}//end of compareTo

This is the input I'm attempting to get from the user
Code:
public static void playGame() throws Exception
{
	System.out.println(hiddenWord);
	System.out.print("What is your guess? ");
	answer2 = (char)System.in.read();
	System.in.read();System.in.read(); //Absorbs Enter Key
	CharSequence answer = new CharSequence(answer2);
	compareTo(answer);
}//end of PlayGame

And this is the error I'm getting from the compiler
Code:
87: java.lang.CharSequence is abstract; cannot be instantiated
	CharSequence answer = new CharSequence(answer2);
                              ^
1 error

Basically, I'm trying to take a char input, and put into a CharSequence, and use the guessThis.contains(z) to tell me if the word contains the letter they input. Then I was going to use the .replaceAll method to replace the ******** with the proper letter. I've got my random word generator working (using a string array and random number generator), and once I get this working, I'll have the program done. And no JOptionPane.

Yes, I know there are more "basic" ways to write this program, but I'm trying to use something new in the 1.5.0 release. But I appreciate anyone who happens to know how to do this, or can point me in the right direction.
 
I don't think this is really the answer you want, but note that CharSequence is an interface not a class, so you definitely can't make a new instance of it. In case you're not familiar with this distinction, an interface just says what methods a class has. It doesn't actually store any data.

What you probably want to do is use a BufferedReader instead of trying to read the characters one-by-one from System.in. If you wrap that input stream in a BufferedReader, you'll get the handy readLine method, which will make it so you get the whole line at once, and you don't have to worry about the "enter" key or anything.
 
Well, I just assumed CharSequence was a class. The user is only inputting one character at a time, so the System.in.read should be fine.

Basically, what do I have to do, in order to use this code?


Code:
public static boolean compareTo(CharSequence z)
{
	return(guessThis.contains(z));
}//end of compareTo

I'm trying to do the program differently than everyone else in my class. And this method looks like a very good way to do it, but I need to know what I can pass to the method.
 
I see. I guess I did sort of miss the point there. It would probably be easier for me to help if you just posted the whole class file. But what might help is to realize that String actually implements the CharSequence interface, which makes sense intuitively. So try CharSequence answer = new String(answer2); for example, though there are more clear ways of stating it.
 
I haven't had the pleasure of working with the 1.5 stuff, but from my understanding, you can call the "java.lang.String.contains(CharSequence z)" method by passing a plain old String as an argument. Since String implements the CharSequence interface, in essence, a String is a CharSequence. Who would have though that a String could implement the CharSequence interface! Man those Java dudes crack me up.

Hope that answers your question.

EDIT, I see someone has already answered. Why oh WHY did I eat the cookie before answering the Thread. :(

Although it was a pretty good cookie :cool:
 
Yes, passing a string to the method works perfectly. And the method does exactly what I want it to do, now if I could just get my methodto work that replaces the hidden letters with the actual letters when they guess correctly, I'll be done.
 
Thanks again for the help, the only reason I didn't post the whole class is because there is another user of these forums who comes here frequently who is in the same section as me. Don't want to give him all the answers... sorry Tim. lol
 
Back
Top