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 Delivering the MVP Using Method Overloading

Instead of using --> if (letter.length() ==0)... and so on on line 25, could I use --> if (letter == "")...?

I first wrote the code as:

if (letter == ""){ throw new IllegalArgumentException ("No letter found"); }

But then I saw Craig using if (letter.length()==0)...

Is it correct to write the code like I did on the first time?

Mark Casavantes
Mark Casavantes
Courses Plus Student 13,401 Points

Hello Ewerton,

There are many ways to do things in programming that are valid. I think you need to use:

if(letter.equals"") {

The == is used for variables and values.

if(number == 6) {

Best Wishes Mark

2 Answers

Liam Trant
Liam Trant
2,918 Points

As a char can either be a letter or an int, in order to correctly handle a scenario where no input was received, Craig points out that "Strings have length". If the length is 0 which is a char, then no input was entered, not even a space. This serves two purposes: the first and most important is that if an entry is valid we can return letters.charAt(0), which gives us our char which is accepted by the original applyGuess method. Secondly, if the String has no length, the exception is thrown with the appropriate message.

Your first attempt will not work because you need to make sure that the new applyGuess(String) method will ultimately return a char so that it can be passed into the original applyGuess method, which accepts a char:

public boolean applyGuess(char letter)

Also, the way your attempt is written checks to see if letters is undefined rather than 0. Undefined does not mean the same thing as 0. The rest of our code at this point doesn't have any logic to handle an undefined letters variable and therefore when nothing is entered the application crashes instead of presenting the user with the desired exception message and the option to guess again.

I hope this makes sense...harder to put into words than I thought it would be :)

Really appreciate the help, Liam! You're awesome!

Sean M
Sean M
7,344 Points

Liam has made a good point that undefined does not equal the same thing as 0. I would recommend sticking with the code below since it shows exactly what you mean.

if (letters.length() == 0)