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

Help TreeHouse Challenge, char and indexOf

This is my task:

I have modeled an assistant for a tech conference. Since there are so many attendees, registration is broken up into two lines. Lines are determined by last name, and they will end up in line 1 or line 2. Please help me fix the getLineFor method.

NEW INFO: chars can be compared using the > and < symbols. For instance 'B' > 'A' and 'R' < 'Z'

This is the pre-writen code:

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

}


I do not understand how i am supposed to do this! Please help me understand how i can complete this challenge.

2 Answers

In this challenge you are asked to :

  • check what is the first letter of lastName ;
  • see if this letter is between A and M or N and Z ;
  • if the this letter is between A and M set line to 1 ;
  • else set line to 2.

Try to solve this challenge with these explanations but if it is too confusing here's the solution with explanations :

public class ConferenceRegistrationAssistant {

  public int getLineFor(String lastName) {

//This is the variable line which must be equal to 1 or 2 according to the first letter of the variable lastName.
  int line = 0;

//This is the if statement which checks if the first letter of lastName comes before N to see if this letter is between A and M.
    if (lastName.charAt(0)<'N') {

      //If this is the case the variable line is set to 1 because the first letter of the variable lastName is between A and M.
      line=1;

    } 

//If this is not the case we can deduce that the first letter of the lastName variable is between N and Z.
else {

      //So we define the variable line to 2.
      line=2;

    }

    //We finally return the variable line returning 1 or 2.
    return line;

  }

}
Ralph Mowers
Ralph Mowers
5,778 Points

Thank you for help. The code works.