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

Lee Reynolds Jr.
Lee Reynolds Jr.
5,160 Points

Counting Tiles

Once again, which seems to start becoming a dishearteningly reoccurring thing lately, I am getting no errors in my code test, yet it will not pass through the challenge. Is there some kind of way that I can test more critically than when I put in my code and try to go through the preview area? I don't want to fill up the forum with a lot of questions that annoy everyone, I just don't know what else to do.

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;

  public ScrabblePlayer() {
    mHand = "";
  }

  public String getHand() {
   return mHand;
  }

  public char getTileCount() {
    mHand.toCharArray();
    for(char count : mHand.toCharArray()){
      System.out.println("There are " + count + "tiles.");
    }
    return getTileCount();
  }

  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;
  }
}
Lee Reynolds Jr.
Lee Reynolds Jr.
5,160 Points

I also tried this code below, and it did the same thing. I really want to learn but I also don't want to be completely hopeless because I really love coding. Please help me Craig, and others.

public class ScrabblePlayer { private String mHand;

public ScrabblePlayer() { mHand = ""; }

public String getHand() { return mHand; }

public int getTileCount() { mHand.toCharArray(); for(int getTileCount : mHand.toCharArray()){ getTileCount++; } return getTileCount; }

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; } }

4 Answers

Hey Lee,

Took me ages to get this one to work but finally got there with this: I'm a beginner so please don't take this as the right answer, but it works :)

public int getTileCount(char tile) { int tileCount = 0;

 for (char letter : mHand.toCharArray()) {
   if (tile == letter) {
   tileCount++;
   }
 }

return tileCount;

}

I think on yours, the line: for(int getTileCount : mHand.toCharArray()) is where the problem is

it should be: for (each letter in the array) do this

so letter is a new character (char) not an int.

Lee Reynolds Jr.
Lee Reynolds Jr.
5,160 Points

I thank you truly for your assistance. I am going to revisit the lesson and see what I missed. I can't have myself not understanding this much about what I'm not doing correct, especially when the answer is literally right there for me.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Try pressing the label under the title of this post with <> for each loop. It will show you other people's question on this challenge. See if you can't figure it out using information from your fellow students' challenges.

(I see you missing the char passed into the method, you are close)

Lee Reynolds Jr.
Lee Reynolds Jr.
5,160 Points

I'll give it a look. I like how you encourage critical thinking and allow time for someone to find their answers. It helps with one learning the ability to research and find out what if the fault in our logic. Is there another area on treehouse that we can do extra challenges on things that we might be a little unsure about still? Like extra homework or something? I wouldn't mind a little extra practice.

Lee Reynolds Jr.
Lee Reynolds Jr.
5,160 Points

I think that I'm going to do the video and all over again. I see where the answers are given in the examples and I think I understand the other areas, but there are some parts that I just don't understand completely yet. "If you can't explain it simply, you didn't understand it well enough." --- Albert Einstein

I can't explain it simply(Or complexly for that matter.).

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Your second example is close. Only count the tile if it is equal to the letter passed into the method.

Lee Reynolds Jr.
Lee Reynolds Jr.
5,160 Points

public class ScrabblePlayer { private String mHand;

public ScrabblePlayer() { mHand = ""; }

public String getHand() { return mHand; }

public int getTileCount() { mHand.toCharArray(); for(int getTileCount : mHand.toCharArray()){ if(getTileCount == tile) { getTileCount++; } } return getTileCount; }

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; } }

I tried this and I got so many errors that I don't know what to do with myself. Where is my thinking going wrong? Have I missed a lesson I need to revisit before these lessons?