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 trialGendarme Docena
1,509 PointsResult of the expression?
Hi, can someone please take a look at my code? I wrote it out correctly but it doesn't want it in an if/else statement. I'm unsure on how it turn it into the "result of the expression.."
public class ScrabblePlayer {
// A String representing all of the tiles that this player has
private String tiles;
private String answer;
public ScrabblePlayer() {
tiles = "";
}
public String getTiles() {
return tiles;
}
public void addTile(char tile) {
tiles += tile;
}
public boolean hasTile(char tile) {
boolean itPassed = answer.indexOf(tile) != -1;
if (itPassed) {
return true;
} else {
return false;
}
}
}
2 Answers
Philip Gales
15,193 PointsWhen I run your code it gives me the following error.
Bummer! While you could definitely solve this using an if statement, try returning the result of the expression.
This tells me that they want you to return the result of the boolean and not use an if statement. I erased the if statement and returned the boolean
public boolean hasTile(char tile) {
boolean itPassed = answer.indexOf(tile) != -1;
return itPassed;
}
I now get the following error:
Bummer! java.lang.NullPointerException (Look around ScrabblePlayer.java line 19)
Line 19 is where you declared your boolean itPassed. This is telling me something is wrong with the variable. I then realized "answer" has no meaning in this class so I changed it to tiles since we are checking if tile is in tiles.
Your final code is below.
public class ScrabblePlayer {
// A String representing all of the tiles that this player has
private String tiles;
private String answer;
public ScrabblePlayer() {
tiles = "";
}
public String getTiles() {
return tiles;
}
public void addTile(char tile) {
tiles += tile;
}
public boolean hasTile(char tile) {
boolean itPassed = tiles.indexOf(tile) != -1;
return itPassed;
}
}
Gendarme Docena
1,509 PointsOh wow, I think I understand it much more now. It definitely worked, thank you so much!
Philip Gales
15,193 PointsGlad it helped! Be sure to mark an answer "best answer" by clicking on the check mark next to add comment.
Blake Larson
13,014 PointsAnother way..
return tiles.indexOf(tile) >= 0;
Bhushan Sabadra
Courses Plus Student 1,646 PointsBhushan Sabadra
Courses Plus Student 1,646 PointsWHATS THE QUESTION CAN YOU PLEASE LET ME KNOW??????