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

solange tuyisenge
solange tuyisenge
3,081 Points

I need help on task challenge"https://teamtreehouse.com/library/java-objects/creating-the-mvp/for-each-loop"

I am getting a can not find symbol error and it says variable mHand of type string. i can not figure out where I went wrong.

Thank you for assisting

Solange

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Solange,

can you post your code?

Grigorij

solange tuyisenge
solange tuyisenge
3,081 Points

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

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Solange,

here a code proposal:

public int getTileCount(char tile){ 
  // to proof a specific char inside mHand you need to give that chat as parameter
  int tileCount=0; 
  for (char tileToProof: mHand.toCharArray()){
    // you cant use hasTile() method on mHand because mHand is a String
    // to compare chars inside a String mHand you need to "convert" String mHand in an array of chars using toCharArray()
    // inside the for loop you want to compare every char from the mHand-array with your argument "tile"
    if(tileToProof == tile){ 
      // condition to increment tileCount
      // if a char from mHandArray equals to the argument char 
      // increment the count
      tileCount++; 
    } 
  } 
  return tileCount; 
}  

Let me know if this was helpful or not ...

Grigorij

solange tuyisenge
solange tuyisenge
3,081 Points

Many thanks Grigorij,

It was so helpful.

Regards,

Solange

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Nice !!!!

Shout out here in the forum if you need more help

Grigorij