Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Creating the MVP Current Progress

Tyler Combs
Tyler Combs
15,525 Points

Stuck on the last challenge, stuck on this one.

After chewing on the last challenge for a while, I was able to get it 95% right, and a nice member of the community helped me fix it the rest of the way. (Basically I was comparing chars with lower case letters. Didn't know it had to be uppercase.) Anyway, on this challenge, I'm just completely stuck. I can't even get my syntax completely right no matter what I try. Review of the video doesn't help me much, because translation of Hangman to Scrabble is apparently really difficult for me.

I just don't even know where to start with this one. This is all I know to do for this challenge:

public int getTileCount() {
   for () {

   }
}

Some guidance and clarification of how to accomplish my goal would be greatly appreciated.

Tyler Combs
Tyler Combs
15,525 Points

Sorry, I may have messed it up when typing it here. I didn't copy it from the challenge.

2 Answers

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

Let me give you direction: you have to write method getTileCount(char tile) that loops through tile in mHand and if this tile as argument equals to tile in hand, we increase counter...Does it make sense?

 public int getTileCount(char tile) {
     // cycle through tiles in hand
     // if tile in hand == tile in argument, then increase counter
     // return counter:
  }
Tyler Combs
Tyler Combs
15,525 Points

Thanks so much for the help! I wrote the following:

public int getTileCount(char tile) {
   int counter = 0;
   for (char tiles: mHand.toCharArray()) {
      if (mHand.toCharArray() == mHand.indexOf(tiles)) {
         counter++;
      }
   } return counter;
}

The only problem is, I was trying different things because I kept getting a syntax error for using "char tile" in my for-each loop, and now I have no idea why this code works. I never previously defined "tiles". Plus, the if statement makes no sense to me either. It feels like a lucky guess rather than a completion.

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

Here is the solution I suggested

public int getTileCount(char tile) {
   int counter = 0;
   // when you write like this then inside loop 
   // variable "tileInHand" will represent one tile
   for (char tileInHand: mHand.toCharArray()) {
      // for every "tileInHand" we compare it to "tile" in argument
      // if tileInHand == tile, we increase counter
      if (tileInHand == tile) {
         counter++;
      }
   }
   return counter;
}

So the question that I'm answering is this: how many tiles of given type are there in Hand?

If mHand = "hand", then getTileCount('a') will return 1, and getTileCount('z') - return 0

Tyler Combs
Tyler Combs
15,525 Points

So, basically, my solution was essentially the same as yours, but I just didn't understand it because I used "tiles" as a quick-fix blind attempt, where I should have used something a little more defining such as "tileInHand" for clarity purposes?

Edit: My problem seems to be the fact that I over-think things. :)

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

Tyler Combs

Well, I tried your solution above, and it does not work for me, unfortunately. I don't know how were you able to pass the challenge.

You see, you have error in this line:

if (mHand.toCharArray() == mHand.indexOf(tiles))

Simply because mHand.toCharArray() is of type char[], i.e. array of chars, whereas mHand.indexOf(tiles) is of type int and "Returns the index within this string of the first occurrence of the specified character or -1 if the character does not occur." Taken from here:

http://www.tutorialspoint.com/java/java_string_indexof.htm

So I don't think that code should work, I don't know what are you trying to achieve or check with this condition...