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

Thomas Agius
Thomas Agius
3,544 Points

In need of Help stuck... keeps telling me that I need to add a method which accepts a char

So I declared the method which would return an integer (Counter) I initialised it in int Counter = 0; I'm using the for loop to accept a char tile for each char in mHand I initialised another int which will run in the loop then I created an if statement which checks if there is a tile in hand, if there is a tile, it will increment mCount finally as it is exiting the loop Counter is assigned the same integer amount and is returned. I'm very new to this any help would be greatly appreciated. See the attached code (getTileCount method)

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

      }
      return Counter;
  }

  public boolean hasTile(char tile) {
   return mHand.indexOf(tile) > -1;
  }
}

5 Answers

Hi Thomas,

The challenge is asking you to create a method that takes a character as a parameter and returns the number of times that character appears in mHand.

I wrote a previous response to this challenge here which may help you out.

Steve.

Thomas Agius
Thomas Agius
3,544 Points

Hi Steve,

Thanks for the help much appreciated! The question was a bit misleading.. I thought I had to enter the number of tiles in hand :))

Thomas

OK - yes, I agree that the challenge is not particularly clear in explaining what it is looking for. I struggled initially with it prior to writing the post in the link.

Going back to your code, though, is there some learning to be done from that? To me, it looks like you are converting the string to a chararray then looping through each character, incrementing a counter then switching that value into another variable, then returning that variable. So this counts the number of characters in a string?

We can do that with the .length() method, perhaps? That will return the number of characters in a string, I think. It'll work on any string as it is a method common to the string class.

That aside, there's some duplication between your two variables, mCount and Counter. (Further side note - mCount isn't a member variable of a class so, perhaps, shouldn't start with an m. And Counter is capitalised - naming conventions for variables suggest they use camel case so the initial should be a lower case letter, thisIsAnAcceptedVariableName, for example. Anyway, the fact that you assign the value of mCount to Counter then immediately return Counter from the function suggests that one of these variables isn't needed, potentially.

However, you've clearly got a good grasp of how Java works; there's no errors in your code that I can see (and I'm not an expert!!). One thing I learned (the hard way!) was to get a clear idea of what I'm trying to do long before I reach for Java (or whatever language). For simple tasks, we can get away with jumping straight into code but for tasks that have more than one stage, many people find it best to write down what we're trying to achieve before getting into the code.

There's formal methods for doing this (I don't know any of them!) termed pseudo-code - I recall using the Vienna Development Method many years ago at university. In fact, I think one of the Professors there had something to do with creating VDM - but that's irrelevant! The point is that proper planning before starting writing code is key to developing good code in whatever language. Being clear on the problem first and then working on the mechanism that needs to operate to solve it helps you make sure you can start writing code at the right time when you are properly clear on what problem you're trying to solve.

I hope all that helps! Keep going - there's lots to learn and it's all good fun.

Steve.

Thomas Agius
Thomas Agius
3,544 Points

Thank you Steve you've been very helpful I was stuck for a week and almost gave up. I tried writing it down before going for coding itself but I'll probably need to plan things better next time.

I'm still a bit shaky when it comes to choosing the right methods and right now most of the time I'm comparing to previous exercises to come out with solutions. I hope I get used to the language soon and start coding a bit more independently.

I admit it's quite challenging at this point but I'm really enjoying coding and looking forward to start using it in my own projects.

Thanks,

Thomas

Keep at it - the more you do, the clearer it becomes. And if you're stuck on anything just shout in the forum. There's always someone about. Feel free to tag me if you want a speedy response - I'll pick that up as soon as I can. Just add a @ before a name and it'll present you with a drop down of options, like: Steve Hunter (although I can't find your name in there - odd!)

Thomas Agius
Thomas Agius
3,544 Points

Thanks again Steve much appreciated!