• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is wrong with my array loop?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!




 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)."
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And does your method work?
 
reply
    Bookmark Topic Watch Topic
  • New Topic