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

Quinton Rivera
Quinton Rivera
5,177 Points

Please tell me why my code did not work when i declare and initialized count outside of the getTileCount method

int count = 0; public int getTileCount(char guessTile){ for(char answerTile: mHand.toCharArray()){ if(answerTile == guessTile){ count++;}} return count;}

why did it only work when i wrote it as I am very confused public int getTileCount(char guessTile){ int count = 0; for(char answerTile: mHand.toCharArray()){ if(answerTile == guessTile){ count++;}} return count;}

Just to make it more readable

int count = 0; 
public int getTileCount(char guessTile) { 
for(char answerTile: mHand.toCharArray()) {
 if(answerTile == guessTile){
 count++;
}
}
 return count;}
Quinton Rivera
Quinton Rivera
5,177 Points

So does that mean i can initialize my counter variable outside of the method and it should work? I apologize but i did not understand your answer. In my java class I am taught to declare variables with global scope

2 Answers

Your code will pass, if the "int count = 0;" is inside of the method. Also watch out i also made some mistakes because I missed/deleted some curly brackets.

The code i posted was the original code from you...is just formatted it to make it more readable

La Gracchiatrice
La Gracchiatrice
4,615 Points

On my laptop I compiled and tested the two snippet of code and the outcomes were the same. Only in the code challege the outcomes are different. I suppose that Treehouse checks the answer against an ScrabblePlayer class with implemetation that differs from what we see . In this unknown implemetation Treehouse could use the count variable in other methods so if you define count as a instance variables other methods can change count value so the test could fail.

public class Main {

    public static void main(String[] args) {
    // write your code here
        ScrabblePlayer1 test=new ScrabblePlayer1("sreclhak");
        System.out.println(test.getTileCount('n'));
    }
}



public class ScrabblePlayer1 {
    private String mHand;

    public ScrabblePlayer(String hand) {
        mHand = hand;
    }

    public String getHand() {
        return mHand;
    }

    public void addTile(char tile) {
        // Adds the tile to the hand of the player
        mHand += tile;
    }



    public int getTileCount(char guessTile) {
        int count = 0;
        for (char answerTile : mHand.toCharArray()) {
            if (answerTile == guessTile) {
                count++;
            }
        }
        return count;
    }
public class Main {

    public static void main(String[] args) {
    // write your code here
        ScrabblePlayer2 test=new ScrabblePlayer2("sreclhak");
        System.out.println(test.getTileCount('n'));
    }
}



public class ScrabblePlayer2 {
    private String mHand;

    public ScrabblePlayer(String hand) {
        mHand = hand;
    }

    public String getHand() {
        return mHand;
    }

    public void addTile(char tile) {
        // Adds the tile to the hand of the player
        mHand += tile;
    }


    int count = 0;
    public int getTileCount(char guessTile) {

        for (char answerTile : mHand.toCharArray()) {
            if (answerTile == guessTile) {
                count++;
            }
        }
        return count;
    }