Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Creating the MVP For Each Loop

Steve DeYoung
Steve DeYoung
1,313 Points

Question about getTileCount method

This is what I have so far but I'm really stuck.. Any help would be appreciated..

public int getTileCount(char tile) { int tileCount = 0; for(char count : mHand.toCharArray()){ tileCount += count; } return tileCount; }

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(char tile) {
   int tileCount = 0;
   for(char count : mHand.toCharArray()){
    tileCount += count;
   }
   return tileCount;
}

}

4 Answers

Here you go:

   public int getTileCount(char ltr){
      int mCount = 0;
      int i = 0;  //loop counter
      for(char letter: mHand.toCharArray()){
      //for (int i = 0; i < mHand.length(); i++) {
         if(mHand.charAt(i) == ltr){
            mCount++; 
         }
         i++; 
      }
      return mCount;
   }

Since you need subscript notation to access the elements in the array, you would need to create a loop counter variable, use it, and increment it each time through the loop.

Steve, here's the function. The key thing here is that you have to compare the letter/tile passed into the function with each letter/tile in mHand.

   public int getTileCount(char ltr){
      int mCount = 0;

      for (int i = 0; i < mHand.length(); i++) {
         if(mHand.charAt(i) == ltr){
            mCount++; 
         }
      }
      return mCount;
   }

But just in case, here's the whole thing:

import java.util.*;

public class ScrabblePlayer {
   private String mHand;

   public int getTileCount(char ltr){
      int mCount = 0;

      for (int i = 0; i < mHand.length(); i++) {
         if(mHand.charAt(i) == ltr){
            mCount++; 
         }
      }
      return mCount;
   }

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

   List<String> list = new ArrayList<String>();

}
Steve DeYoung
Steve DeYoung
1,313 Points

Thanks so much! Is there a way to write this with a for-each loop?

Steve DeYoung
Steve DeYoung
1,313 Points

Wow thanks so much!!! This site sure is awesome!