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

Simon Andersson
Simon Andersson
6,880 Points

Increment a char?? I'm confused, please help..

Hello, I'm doing this exercise but I'm totally lost again. It says I'm supposed to create a tileCount and increment it if it matches. For starters, I have never played scrabble so I have no clue how it works. Usually i would just increment as following

int increment = 0; increment++;

Anyhow, here's the exercise description in case you can't see it:

So back to that ScrabblePlayer. I found that it's not enough to know if they just have a tile of a specific character. We need to know how many they actually have. Can you please add a method called getTileCount that uses the for each loop you just learned to loop through the characters and increment a counter if it matches? Return the count, please, thanks!

However, I can't increment a char, and I can't convert an int to a char either, as far as I've learned. I have to return a char however, confused. Help is appreciated. =)

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;

  public ScrabblePlayer() {
    mHand = "";
  }

  public String getHand() {
   return mHand;
  }

   public char getTileCount() {
   char tileCount = '';
   for(char currentTile : mHand.toCharArray()) {
     if(mHand.indexOf(currentTile) >= 0) {
      tileCount++; 
     } 
   }
    return tileCount;
  }

  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

I'm typing from my iPhone so I don't know how this will go but you want to set your method to return an int so:

public int getTileCount(char tile){

}

Next inside your method body you want to create a count variable: int count=0;

Next you want to do a loop that will determine if the argument passed in matches a tile in variable mHand: for(char letter : mHand.toCharArray()){

}

Next, inside the for loop you need to do an if statement to determine if it matches:
if(letter == tile) count++;

Then after the for loop you need to return your count variable.

Simon Andersson
Simon Andersson
6,880 Points

Thanks for such a quick answer. I just missunderstood the excercise, I totally understand what's going on now and I got it working. Thanks a bunch. :)

I noticed that you tried to increment a char variable. It's kind of like saying a + 1. You need to have an int (integer) variable that can be incremented. When you say count++ that means whatever the current number is you add 1 to it so if you have 3 stored in the variable and you do the ++ to it after the loop finishes 1 will be added and you will have 4.

Okay cool you're welcome. The assignments are tricky sometimes :)

Simon Andersson
Simon Andersson
6,880 Points

Yeah I figured I couldn't increment a char, but I thought they wanted me to return a char when they in fact wanted me to make the function accept a char as a parameter and return an int, so I got confused. Anyhow, thanks again, now I can move on, cheers! :)