• Advertisement

Archived

This topic is now archived and is closed to further replies.

tokens (Java)

This topic is 5996 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Quick question: I''ve obtained a word from a file in java and need to split in into individual characters. How can I split on each char? I thought about simply putting a space in between each letter in the .txt file but I though that would be cheating.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Cheers for that Dean. Much simpler :-)

now im trying to store these individual chars into a vector for furture access.

for( int booga = 0; booga < randomWord.length(); booga++ ,dashStartX += 30)
{
char letter = randomWord.charAt(booga);
g.drawString(""+letter, dashStartX, 100);
letterman.addElement(letter);
}

error says vector cannot be applied to characters. Any suggestions people? (try an keep it simple!! :-) )

Share this post


Link to post
Share on other sites
Vector can only contain Objects, meaning that you can''t stuff a regular char into it, you have to wrap the char in a Character object.

  
for( int booga = 0; booga < randomWord.length(); booga++ ,dashStartX += 30)
{
char letter = randomWord.charAt(booga);
g.drawString(""+letter, dashStartX, 100);
letterman.addElement(new Character(letter));
}

There''s probably some easier way to do it though. Why not simply save the string and index into that later; instead of saving the string as a vector of characters? It seems a little unecessary.

Share this post


Link to post
Share on other sites
Thanks for that, it worked!

Yeah sounds like a good idea, but how would I do an If statement comparing the users input against individual chars? Could you give an eg?

Share this post


Link to post
Share on other sites
I''m not sure what you mean, can you give some more info. Like:

What does the user input represent?
What does the characters in the strings represent?
(i.e. why do you want to compare them)

Comparing a char to a char in a string is as simple as:
  
char c = getUserInput(); // Whatever...


if (c == someString.charAt(someIndex))
{
// Do something...

};

Share this post


Link to post
Share on other sites
hehe real time message board.
Its a hangman game. the user types in a character into a textfield (playerInput). After that it goes to a check letter procedure.

If that letter is in the word It shows up in the correct place on screen

else draw the next part of the hangman. (still having trouble using Graphics g in actionPerformed, hard to get around it)

Share this post


Link to post
Share on other sites
Sorry to break the real-timeness (if that's a word )

First of all, please don't be offended if I seem patronizing .

I get the impression that you're pretty new to this (programming in general), so perhaps you should start by making a text-mode hangman game. That way you can concentrate on making the game logic work, and displaying the words and the gallows will be easier (just System.out.print(...) etc.). After you have the game working you can always convert it to use graphics for its output (it won't be too hard, since the output is simple).

Now to your more specific question(s). I'm going to describe *one* way to do this. I'm not in any way claiming that this is the "right" way to do things, just something I thought of while reading your posts, i.e. I've never actually made a Hangman game, but perhaps you will get some ideas from this.

You could have a regular String object that stores the correct solution to the hangman puzzle (from here on called 'correct'). Then you have a StringBuffer object (from here on called 'correctSoFarBuffer')that is initialized to be the same length as 'correctString', but containing only '_' characters. It represents the characters of the word that the user has so far guessed correctly. Whenever you get a character from the user (called 'guessChar'). you search 'correctString' to se if the char matches, and in every match (not just the first one) you update the character in 'correctSoFarBuffer' with the corresponding index to be 'guessChar'. If 'guessChar' is nowehere to be found in 'correctString', then increase an error variable or something, indicating that a new piece of the gallows has been added.

Then you "draw" the gallows (using ASCII art ) and print the 'correctSoFarBuffer' to the screen, showing the user what parts of the word he has currently guessed.

If you want to you can have a second StringBuffer that contains all the erroneous characters the user has typed, and print this string to remember the user what characters he has already guessed at (wrongly).

e.g.
correctString contains "foobar"
correctSoFarBuffer contains "______" (six '_' characters)
If the user then inputs an 'a' character, then 'correctString' will be searched and found to contain an 'a' at index 4, thus correctSoFarBuffer is updated to contain an 'a' at index 4.

Now correctString contains "foobar" (it never changes) and
correctSoFarBuffer contains "____a_".

To check whether the user has won, simply compare 'correctString' and 'correctSoFarBuffer' (using equals, not '==').

Perhaps a bit too much unnecessary detail, and the names aren't so good, but I hope you get the point . There are many other ways to do this. Perhaps you should use a Vector after all, to learn Vector-manipulation. I don't know, feel free to ignore everything I've said

Edited by - Dactylos on September 24, 2001 12:59:19 PM

Share this post


Link to post
Share on other sites
Yeah thats basically what ive be trying to do, ive almost finished it anyway (with graphics) probably be done in a few days.
And the graphical element isnt too difficult, i''ve just got a simple drawLine function for each line, If userInput !=letterInWord.

cheers.

Share this post


Link to post
Share on other sites

  • Advertisement