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

TJ von Stein
TJ von Stein
1,188 Points

Help with getForLine method

I cant get the first letter of the last name to compare if it starts with A-M

ConferenceRegistrationAssistant.java
public class ConferenceRegistrationAssistant {
  private String mLastName;
  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 */
      char name = lastName.indexOf(0);
    if (name=='A'||name=='B'||name=='C') {
      line = 1;
    return line;
    } else {
    line = 2;
    return line;
    }
  }
}
TJ von Stein
TJ von Stein
1,188 Points

any help would be greatly appreciated, thank you

David Axelrod
David Axelrod
36,073 Points

Giving it a go right now! Be back in a bit

1 Answer

David Axelrod
David Axelrod
36,073 Points

Here we go!

2 things!

Since we want a char value, we have to use the charAt() method rather than the indexOf() method

now that we have the char, we can use it to compare its value to 'M'. Yes you could chain together a lot of or statements but, going with the mantra of not repeating ourselves, using if(firstLetter < 'M'){...} works too!

Hope this helps!

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;
    char firstLetter = lastName.charAt(0);
    if(firstLetter < 'M'){
      line = 1;    
    } else {
      line = 2;
    }

    return line;
  }

}