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

I have no idea how to do this challenge...

I've been watching these videos and for the past 3 weeks of doing Java, I'm doing what he says in the videos but literally absorbing none of it, I can't conceptually understand Java at all. At this point I'm thinking of Giving up and doing Python or Ruby instead for I've heard those languages are easier. at least until I have a better concept of programming in general.

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

3 Answers

Hendrik Heim
Hendrik Heim
1,012 Points

Hey Nick, don't surrender yet!

public int getTileCount(char hand){

    int count = 0;

    for(char tile : mHand.toCharArray()){
      if(tile == hand){
     count++; 
      }
    }

    return count;


  }

if any further explanation is needed tell me.

I read it but make no sense of it can you give me an explanation line by line if you can?

Hendrik Heim
Hendrik Heim
1,012 Points

Your task is to write a method, which has a parameter of type char, since you want to count how often this char is contained in your String / Char Array.

after this, create a local variable, name it as you want. I took counter because thats what the variable is supposed to do!

now you iterate over every Char in your mHand string by building an Char[] Array of mHand with "toCharArray()" which is provided by .NET Framework.

now just check wheather your current Array Item (tile) equals your inserted parameter. if so add +1 to counter. Afterwards the program moves to the next Array Item and repeats this process.

Makes a little bit more sense, still hard for me to grasp, I think I'm going to learn Ruby, and come back to java at a later time when I learn how to absorb the different concepts better.

Java is a tough language, no doubt, but there will come a time when every thing clicks in to place. I struggled super hard through the first couple of lessons. In fact i struggled so hard i was just copying and pasting answers for the challenges. Eventually i realized that all the info i needed was not all available from just the videos. I started branching off and studying from links in the teachers notes, looking over java documentation, googling what i didn't understand, and taking notes on the things i found. Suddenly and quite magically, things started clicking. Don't give up yet understanding java could just be a lesson away!