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 Prompting for Guesses

Tyler Combs
Tyler Combs
15,525 Points

Please help, I cannot seem to pass this challenge.

I keep getting either "Bummer! I entered Anderson and expected to get back line 1..." or "I entered Zimmerman and expected to get back line 2..."

Here is my code:

public int getLineFor(String lastName) {
    /* If the last name is between A thru M send them to line 1
       Otherwise send them to line 2 */
    int line = 0;
    if (lastName.charAt(0) >= 'm') {
      line = 1;
    } else {
      line = 2;
    }
    return line;
  }

Can someone please explain to me why this code does not work, and please help me get it right? I don't necessarily want "your code should read ___" type of answers, I would much more appreciate an explanation of why my code does not work and how I can correct it myself.

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Short answer? Your people are all going into the incorrect lines. Read the hint at the bottom of the challenge. A is less than Z. Also, I believe you should be comparing to upper case letters. Try it again with these hints! :sparkles:

Tyler Combs
Tyler Combs
15,525 Points

Thanks so much for the quick reply and for your help! I completed the challenge by changing the ">" to "<", and by changing the reference letter from lowercase to uppercase. Now it raises another question for me. Wouldn't this create a bug? What if the user inputs an all lowercase name? Is there a way to ignore case for this method, kind of like ".equalsIgnoreCase()"?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

You're right, it would create a problem. I'm not a Java expert, but I would say that you could get around this by first converting the entire string to lower case with the toLowerCase() method and then read the charAt() of that string. That way the letters would all be the same capitalization. Then set your line and return it :smiley:

Tyler Combs
Tyler Combs
15,525 Points

You deserve cookies!! :D