*
The moose likes Java in General and the fly likes What is wrong with my array loop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is wrong with my array loop?" Watch "What is wrong with my array loop?" New topic
Author

What is wrong with my array loop?

Ryan Callen
Greenhorn

Joined: Nov 29, 2012
Posts: 22
I wrote a method which reads in a string and an array of letters. The method then returns the string with "_" replacing all of the characters in the string EXCEPT for the characters that match up with any of the characters in the array. Essentially, it is part of a hangman game. The problem I'm having is that method is just converting the entire String into "_"'s. HELP!




Joe Ridener
Ranch Hand

Joined: Jul 16, 2012
Posts: 86
Not sure how your doing it. We can only see that method. I am assuming that word1 is an instance variable that is a String. The syntax of this operation is actually very confusing. I think you might want to take a step back and re-design your program after putting a little thought into the simplest way to accomplish what it is your trying to do. You have three loops one that initializes a char array another for and a while loop. There is a simple way of doing this... let me suggest a couple algorithms to simplify this.

char[] charArray = str.toCharArray(); //<----returns a char array as opposed to what you did


your code...
char[] check = new char[word1.length()];

for(int i=0; i<word1.length(); i++){
check[i] = word1.charAt(i);
}


does the same thing...


Next your code...

for(int i=0; i<check.length; i++){

int j=0;
while(j<guesses.length){
if( check[i]==guesses[j] ){
check[i]=word1.charAt(i);

}
else
check[i]='_';

j++;


}

}

Not the biggest deal in the world...doesn't make a difference outside of convenience for the programmer. But the reason for for loops over while loops is that they provide a space to add a 'counter controlled variable' before the first semi-colon within the parenthesis. And the value of that variable is destroyed after the loop is terminated as opposed to creating a field for the method as int j=0; then using j in a while loop...use two for loops its a little simpler to read and write.

Also check out String constructor

String(char[] value)
Allocates a new String so that it represents the sequence of characters currently contained in the character array argument.

instead of iteration through a for try this syntax in place of it

return ret;

return new String(check); limits overhead operations of iteration in your code.

I would have to look at all your code to tell you exactly what the problem is. Perhaps a custom data structure ( regular class) that represents a character in the hangman with a boolean variable that represents if that particular character has been guessed. This is my suggestion as checking a string and performing these operations is kind of complex and difficult. To simplify create a class/dataStructure for each letter of the hangman string.

class Letter
{

boolean guessed = false;//variable is init'd at false until it guessed... use this to determine if the letter or _ will apearin the display
int indexOfThisLetterInSentence;//maintain a copy of its position
char letter;//and the letter that it represents


}
Ryan Callen
Greenhorn

Joined: Nov 29, 2012
Posts: 22
Okay. I made those changes. The context of the method isn't too important because it is pretty self sufficient within the program. So what would be the overall easiest way to write this method? The specification for it are as follows: "Returns a String containing the word, with unguessed letters represented by an underscore, _, and guessed letters revealed. For readability, each character is followed by a space. For example, if the word were ANIMAL, and the letters A and M have been guessed, then "A _ _ M A _ " would be returned (note the trailing space)."
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
And does your method work?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: What is wrong with my array loop?