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 trialiftekhar uddin
4,194 PointsFrom the hand they had they checked 'n'. Expected outcome 0. Actual outcome 1.
public int getTileCount (char letter){ for(char tiles: mHand.toCharArray()) { if(letter == tiles) { count++; } } return count; }
i have also tried
public int getTileCount (char letter){ for(char tiles: mHand.toCharArray()) { if(mHand.indexOf(tiles) == mHand.indexOf(letter)) { count++; } } return count; }
why is it giving me that outcome. it shouldn't give me 1. I feel really stupid. please help.
public class ScrabblePlayer {
private String mHand;
public int count = 0;
public ScrabblePlayer() {
mHand = "";
}
public String getHand() {
return mHand;
}
public int getTileCount (char letter){
for(char tiles: mHand.toCharArray()) {
if(letter == tiles) {
count = count + 1;
}
}
return count;
}
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;
}
}
John Tasto
21,587 PointsCan we see the rest of your code? Specifically, the line where you call getTileCount()
? Do you ever put anything into mHand
?
Java has block level scoping, which means there is a new scope inside each set of {}
. You are able to access variables outside the block as long as you don't declare a new variable with the same name. So when you declare int count = 0;
inside the for
loop, the new count
shadows the count
variable in your class. Once the for
loop ends, that new local count
is destroyed, and the count
you then return from the method refers to the class level count
.
So that really didn't fix your problem, it only made it work for one specific case. Somehow count
is getting incremented once inside the for
loop, and when you declare a new count
inside the for
loop you are just incrementing a different local count
and returning the class level count
.
iftekhar uddin
4,194 Pointsthey never let me call getTileCount(). It was a part of a quiz where they ran the program. The tiles that were put into mHand were automatically generated ones that the quiz ran.
And if that's the case then i still have no idea why it worked =(. I learned more about the variables though. The thing is that I already knew that about the scopes and that's why I declared a class level int. But again like you said it incremented when it wasn't supposed to. There is no logic that would have told it to and I even set the class int object to 0 to make sure it didn't start with int = 1. I'm assuming it may have been a bug but if it isn't i'd love to know what i did wrong so i don't make the mistake again.
2 Answers
Craig Dennis
Treehouse TeacherYou kept the count as a variable in the class. I create an instance and use it multiple times, so the count field grows in that instance. It doesn't belong at the field level does it? I mean when that method is over, do you really want to know that count?
Let me know if that makes sense! Hope it helps!
John Tasto
21,587 PointsI was also thinking it should be declared in the method, not in the class, and certainly not public. But that doesn't solve why it gets incremented in the for loop. If mhand
is empty, the for
loop shouldn't even run a single cycle, so how could count
be incremented?
iftekhar uddin
4,194 PointsDoes the count get incremented every time I make another instance in another method because i made it a public variable in that class? If so then that makes so much more sense now.
John Tasto
21,587 PointsThis works:
public int getTileCount (char letter) {
int count = 0;
for (char tile: mHand.toCharArray()) {
if (letter == tile) {
count++;
}
}
return count;
}
It must have something to do with count
being declared at the class level. If Treehouse runs several tests, one of them might actually add tiles and expect the count to be greater than 0, then if another test runs and expects it to be 0, it is still referencing the old value.
There must be more to it than that though - as iftekhar uddin said in his first comment, if he bypasses incrementing that class level count
by creating a for
loop scoped count
, it passes Treehouse's tests.
Regardless, I'm certain the above code is what they're looking for.
iftekhar uddin
4,194 PointsYep i tried that code and it worked which i put in my update. But it was just weird how if the variable was in the class level it would increment once even though the loop doesn't go through.
Craig Dennis
Treehouse TeacherI run several tests on the code and change the hand.
iftekhar uddin
4,194 Pointsiftekhar uddin
4,194 Pointsdevelopment: i got it to work but only when i put int count = 0; after the for loop but before the if statement....why did that work? why did that fix my problem? My brain is having more of a meltdown than it already was.....