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

error: entered Zimmerman and expected to get back line2 as Z comes after M

Why did I get a 'entered Zimmerman and expected to get back line 2 as 2 comes after M' error message? I entered the following code inside the method: int line = 0; if(lastName.indexOf(0) <= 'M'){ line=1; return line; } if(lastName.indexOf(0) > 'M'){ line=2; }

return 0;
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'){
      line=1;
      return line;
    }
    if(lastName.indexOf(0) > 'M'){
      line=2;
    }

    return 0;
  }

}

2 Answers

deckey
deckey
14,630 Points

Hi David, your mistake is at lastName.indexOf(0) method... String.indexOf(Char) method is searching for a first occurence of that letter within a string. So, in your case, lastName.indexOf(0) is actually searching for zero which is wrong to begin with.

What you wanted to use I guess is String.charAt(Index) method.

good luck! Deckey

deckey
deckey
14,630 Points

Also, you won't need two 'if' statements, maybe if - else could do the trick. good luck!

Matthew Barrus
Matthew Barrus
16,731 Points

also, when line equals 2, that is never being returned. It is instead returning 0. You could change "return 0;" to "return line;" and remove the return statement in the first if. Or to make it real succinct:

public class ConferenceRegistrationAssistant {

public int getLineFor(String lastName) {
    if (lastName.charAt(0) <= 'M') return 1;

    return 2;
}