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

Avi Tsipshtein
7,461 PointsFor each loop task error: possible lossy conversion from int to char
Hi All,
Link to task: https://teamtreehouse.com/library/java-objects/creating-the-mvp/for-each-loop
My Code:
public class ScrabblePlayer {
private String mHand;
public char getTileCount() {
int count = 0;
for (char letter: mHand.toCharArray()) {
count++;
}
return count;
}
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;
}
}
For some reason it won't allow to use int inside a method that was declared to pass char. But if I try to make the method pass an int like this: public int getTileCount() I get an error that I have to pass a char in the method. The task asks specifically to create a counter and increment it, so I have to use and int.
How do I solve this issue?
Thanks,
5 Answers

Simon Coates
28,695 Points public int getTileCount(char x) {
int count = 0;
for (char letter: mHand.toCharArray()) {
//add test
count++;
}
return count;
}
THe method needs to accept a character (see above), needs to test for that character (you'll need to provide implementation, though you should use ==). THe problem was you were converting an integer to a smaller type (without using an explicit cast). TO fix this, i changed the method to return an int. See if that helps. See how you go and if necessary, i can post the complete code later.

Simon Coates
28,695 PointsSuggest
if (letter == x) {
not
if (mHand.indexOf(letter) >= 0) {
What you're doing is testing if a letter is in the string you got it from. It will be true for each character. You want to test each character against the character passed in as a parameter. A lot of people see indexOf and try to use it, rather than ==. Using == is okay for comparing primitive values.

Avi Tsipshtein
7,461 PointsThanks Simon,
I updated my code to this:
public int getTileCount(char x) {
int count = 0;
for (char letter: mHand.toCharArray()) {
if (mHand.indexOf(letter) >= 0) {
count++;
}
}
return count;
}
Now I get this error: The hand was "sreclhak" and 'e' was checked. Expected 1 but got 8. Looks like the if statement is ignored and count is incremented no matter what. I think the if statement is correct. Can you see what I am doing wrong?
Thanks,

Avi Tsipshtein
7,461 PointsThanks Simon, it worked.
But if what your are saying is true, shouldn't this code work as well?:
public int getTileCount(char x) {
int count = 0;
for (char letter: mHand.toCharArray()) {
if (mHand.indexOf(x) >= 0) {
count++;
}
}
return count;
}
Thanks,

Simon Coates
28,695 Pointsno. It just repeatedly tests that the received letter features in the string. You can use indexOf, but it's more complex than testing on equality. To illustrate the problem, i wrote a demo:
class Main {
public static void main(String[] args) {
String mHand = "abcdefz.";
char x = 'z';
int count = 0;
for (char letter: mHand.toCharArray()) {
System.out.printf("Looking for %s in %s\n", x, mHand );
if (mHand.indexOf(x) >= 0) {
count++;
}
}
System.out.println(count);
}
}
produced:
Looking for z in abcdefz.
Looking for z in abcdefz.
Looking for z in abcdefz.
Looking for z in abcdefz.
Looking for z in abcdefz.
Looking for z in abcdefz.
Looking for z in abcdefz.
Looking for z in abcdefz.
8
to use indexOf, you'd need to isolate the individual character to test on. You could do this using substring, or just get a string from the character. A demo:
class Main {
public static void main(String[] args) {
String mHand = "abcdefzz.";
char x = '2';
int count = 0;
for (char letter: mHand.toCharArray()) {
String converted = ""+letter;
if (converted.indexOf(x) >= 0) {
count++;
}
}
System.out.println(count);
}
}

Avi Tsipshtein
7,461 PointsThanks Simon,
Please help me setup a local IDE. What IDE are you using?
Thanks,

Simon Coates
28,695 PointsEclipse is pretty popular, but some of the java courses at treehouse use intelliJ (eg https://teamtreehouse.com/library/local-development-environments specifically covers getting to know this IDE) as it's a similar product to android studio.