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

Shubham Modi
Shubham Modi
5,799 Points

I barely understand the question

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.

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

}

3 Answers

Yes, sometimes the questions can be a bit difficult to unravel. What they want, for the returned value, is an int. They want the function to say, given a last name, whether the person should be in line 1 or line 2, so the function has to return either a 1 or a 2:

 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;
  }
Chaz Hall
Chaz Hall
1,970 Points

Would:

if (lastName.charAt(0)<'M') just be A-L since you said <'M' and not <= 'M'

?

Chaz, not sure I understand your question. The charAt() function returns the char at the index specified as a parameter. So charAt(0) returns the first char in a String. (Remember Java indices always start at 0.)

So if the last name is Smith charAt(0) would be 'S'. If the last name were Jones charAt(0) would be 'J'.

The reason you can use the < operator here is because under the hood chars are ints (in fact, their ASCII values). So 'A' (65) is less than 'M' (77).

Chaz Hall
Chaz Hall
1,970 Points

I'm sorry I wasn't clear.

A=0 B=1 C=2 D=3 E=4 F=5 G=6 H=7 I=8 J=9 K=10 L=11 M=12

If it's less than M then it would go to line =1. But what if your name is Malfoy? My understanding is that in that code M-Z would go into line 2 unless the code was like this: if(lastName.charAt(0)<='M') //which is the less than or equal to.

I'm just trying to understand if that logic is correct is all :)

Close. But in ASCII A = 65, B = 66, etc.

Re Malfoy, it says

if (lastName.charAt(0) < 'M')

Since 'M' (77) is not less than 'M' (77) Malfoy ends up in line 2.