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

What am I supposed to return at the end, how would it look.

It keeps saying that it's missing a return statement but where

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 String getTileCount() {
    for (char tile: mHand.toCharArray()) {
      if(mHand.indexOf(tile) >=0) ;
    }
  }
}

2 Answers

deckey
deckey
14,630 Points

Hi, your last method 'getTileCount' is declared to return a String, but it's not returning anything. But in any case, what you should get from that method is a number, not a string, right? Try defining a counter before the loop, then increment it for each hit inside the 'if' condition. At the end of the loop, this should give you total number of tiles in hand.

Hope it makes sense, without revealing too much ;) good luck!

rydavim
rydavim
18,814 Points

Disclaimer: I'm not particularly familiar with Java, but I know some other languages with similar structures. However, I think I've worked out the answer if you get really stuck.

Below is some psudo-code to help you get started. You'll need to replace comments and all-caps notes with code.

// Create a function to return an integer with the number of a particular tile in hand.
  public TYPE getTileCount(ARGUMENT) { // Your function needs to accept a char as an argument.
    // Create a counter to keep track of how many tiles.
    for (char ITERATOR: mHand.toCharArray()) { // You'll need an iterator variable different from your passed in argument.
      if(EXPRESSION) { // Check if the current tile is the same as the tile passed to the function.
        // If the current tile matches, increment the counter.
      }
    }
    // Return the final count of matching tiles.
  }