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 For Each Loop

Got me again. I am stuck....

It's getting hard for me now. I may just need to start copying and pasting these examples into another IDE to test stuff out more.... I think I will also get repel now...

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;

  public ScrabblePlayer() {
    mHand = "";
  }

  public String getHand() {
   return mHand;
  }

  public void addTile(char tile) {
    // Adds the tile to the hand of the player
    mHand += tile;
  }

  public boolean hasTile(char tile) {
   return mHand.indexOf(tile) > -1;
  }

   public String getTileCount(){
    for (char tile: mHand.toCharArray()){
       String count = mHand.indexOf(tile) > -1;
      //if count.indexof(tile)){
    }     return count;
}

1 Answer

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi trell,

don´t give up, coding ninja !!!

This challenge wants you to create a method that returns a number "count" that represents how many special characters are inside of mHand.

Let us look at your code:

  public String getTileCount(){
// the return type should be an integer
// because you want to return a number of tiles that are in mHand
// don´t forget a parameter "tile"!
// the parameter is the char we are looking for

    for (char tile: mHand.toCharArray()){
       String count = mHand.indexOf(tile) > -1; 
// here you are trying to store a boolean into a Sting
      if count.indexof(tile)){ 
// you forgot an opening bracket for the if loop
// indexOf returns a number that represents the index position of tile inside count
// but count is not needed to be analized ... mHand should be analized :)
    }    
 return count;
// count is not initialized and empty
// and return type should be an integer
}

Here my commented code suggestion:

public int getTileCount(char tile){ 
  // to proof a specific char inside mHand you need to give that chat as parameter
  int tileCount=0; 
// you will need an integer to incremet
// set the value to 0
  for (char tileToProof: mHand.toCharArray()){
    // you cant use hasTile() method on mHand because mHand is a String
    // to compare chars inside a String mHand you need to "convert" String mHand in an array of chars using 
    //  toCharArray()
    // inside the for loop you want to compare every char from the mHand-array with your argument "tile"
    if(tileToProof == tile){ 
      // condition to increment tileCount
      // if a char from mHandArray equals to the argument char 
      // increment the count
      tileCount++; 
    } 
  } 
  return tileCount; 
}  

Let me know if you are stuck and happy coding ....

Java is a beatifull beast ... don´t forget it :)

I need to be careful of my data types. I was close. You are right! Java is definitely beautiful but also Lions! Got to keep working on these cat training skills. Getting some scratches. Got popped with one of my chewing gum bubbles. Thanks again, have a good day.