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

Cannot understand where comparison fails, can anyone help ? Thanks

Iterating with for each loop , cannot see where error is

ScrabblePlayer.java
public class ScrabblePlayer {
  private String mHand;
  private int count = 0;

  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(char tile){
for (char letter: mHand.toCharArray()) {
    if (letter == tile) {
    count += 1;
  }
}
    return count;
  }
}

3 Answers

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hello, you declared your count as a global variable, so that it's going to retain it's value throughout different method calls, you need to declare your count variable within the method so that any increases in the count is reset after the value is returned, something like.

  public int getTileCount(char tile){
    int count = 0;
    for (char letter: mHand.toCharArray()) {
    if (letter == tile) {
    count += 1;
  }
}

Also, you'll want to delete your global variable count at the top of your class, you only need it within the class.

Right, I have to review some basic concepts, just one question, the code checker said that it was returned 1 and printed one string and one char that wasn't in this examplestring but this couldn't happened anyway because there was no equality

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey ,

because the variable was global, what happened in this case. This method is ran multiple times on the background, so say it's ran once and you're checking for 'c' it would first be tested in the background on something on 'cat' and it would increase the count variable to one, which the global variable would retain as data, and then it would run another test using the method, and if it was something like 'bird' the count still holds the value from the initial test, however bird would have no c in it, so your program would return that the count was one, when the testing code was expecting the value of 0.

Basically in the background the code is being ran multiple times over and over to make sure that your method passes the tests, and with a previous value being kept in count, it would soon come to those that would return back the incorrect number.

You were close, it was a simple error that caused this, and one that was an easy fix.

Thanks Rob, now it's clear