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

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!