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 Comparing Characters

David Barnes
David Barnes
919 Points

In ConferenceRegistrationAssitant why do I get the error message: "I entered A and got Anderson"?

I typed in 'if(lastName.indexOf(0)<=M{return line = 1;}' thinking that line one will be returned but the response I get is 'I entered A and got Anderson'. This is a challenge in the Java Objects chapter. Thanks.

ConferenceRegistrationAssistant.java
public class ConferenceRegistrationAssistant {

  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.indexOf(0)<='M'){

      return line=1;
    } 

    return line=2;
  }

}
Matthew Barrus
Matthew Barrus
16,731 Points

indexOf returns the position of the provided parameter (in your case, 0). Instead, use lastName.charAt(0) to get the first letter of the string.

The 2nd issue is that you are not actually returning the value of line, but instead returning whether the assignment of a value to line was successful. So you should instead set line and then return line:

line = 1; return line;

or just return the value. There really is no need to create a variable when you can just return a 1 or a 2:

return 1;

David Barnes
David Barnes
919 Points

Hi Matthew. Thank you for your response. Rather than giving me the bare minimum this helps me to become a better coder. This is far more useful to me. Cheers.

Matthew Barrus
Matthew Barrus
16,731 Points

I gave you some bad information. When I said that you shouldn't do "return line=2;" because it won't actually return the value of line, I was wrong. It does in fact return the value of line. But, it is still unnecessary to store the value in a var if it's just being returned right there. you can just "return 2". Sorry for the confusion!