Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Creating the MVP Comparing Characters

I have modeled an assistant for a tech conference. Since there are so many attendees, registration is broken up into two

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 1 = 0;
    int line 2 = 0;
    String ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    if(ABC.indexOf(lastName.charAt(0)) <= 12){
      line += 1;
      return line 1;
    }else{
      line2 += 1;
      return line 2;
    }

  }

}

Whats wrong with my code I trey to use

lastName.charAt(0)

to find the first char of lastName

then I insert it into indexOf to see what that chars index is in ABC

and then comparison

<= 12

the problem I think is with returning lines...waahaa

1 Answer

Thomas, nice try, but you are doing way too much work:

  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;
  }

Just check the first letter of each name and if its 'M' or less it's line 1, otherwise line 2.