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

Ranjen Naidu
Ranjen Naidu
2,474 Points

Current Progress Challenge

Bummer ! Did you forget to create the method getTileCount that accepts a char

This is what i got for the code attached . Can i know what is the problem and how to solve it.

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 int getTileCount(){
  int counting = 0;
  for(char letter : mHand.toCharArray()){
    int displayNum = 0;
    if(mHand.indexOf(letter) >= 0){
    displayNum += 1;
    }
    counting += displayNum;
  }
  return counting;
  }
}

3 Answers

Ranjen Naidu
Ranjen Naidu
2,474 Points

Thanks but actually my algorithm was wrong in a first place My understanding about the question was not clear . This will actual make the code works

getTileCount.java
  public int getTileCount(char tile){
  int counting = 0;
  for(char letter : mHand.toCharArray()){ // get each of the character in the string[a,b,c,d,e]{
    if(letter == tile){
    counting++;
    }
  }
  return counting;
  }
Wendy Peralta
Wendy Peralta
11,203 Points

It seems like you forgot to add a parameter to your getTileCount method. The error message is saying getTileCount should accept a char. So it should look like:

public int getTileCount(char tile)

Can you please explain the answer above. How and when do i know when to use integers because i had completely gone astray on this question.

Wendy Peralta
Wendy Peralta
11,203 Points

You don't need to use integers for this particular question. The int in the method "public int getTileCount" is the type of variable that will be returned. (char tile) is what kind of variable the method accepts to do the work.