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

Why isnt my code working?

Why isnt my code working?

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 */
   String line1 = "";
   String line2 = "";
   char firstLetter = lastName.charAt(0);

  if ( firstLetter >= 'A' && firstLetter <= 'M'){
   line1 += firstLetter;
  }
  else {
    line2 += firstLetter;
  }

  }

}

3 Answers

HI Lorcan,

Let's see what we can do for you here.

The question is asking you to send people either down line one or two depending on the first letter of their last name.

You've correctly thought an if statement would do the trick. First, though, we need to get the first letter of the surname. The name is passed in as the method parameter called lastName - it is a String object. So we can work with that. A String object has a method called charAt() which lets you get, you've guessed it, the character at the number you choose in the brackets. So, to get the first letter, this being Java, you ask for charAt(0) - that'll be the first letter.

Right, so we have that - so we now need to compare it to the letters that decide which queue to use. Any letter from A to M goes to the first queue - the rest go to the other. We can compare letters just like numbers, thankfully, so we don't have to go through every letter of the alphabet! So, A is less than M but M is also OK to go to queue one. So everything from A to M can be expressed as any letter that's either M or less than it.

Now we're getting somewhere.

if(lastname.charAt(0) <= 'M') is our condition - we just need to send them to line one.

if(lastName.charAt(0) <= 'M') {
    line = 1;
} else {
// do something else
}

So, what to put in the else bit? The only other option - there's only two lines of people:

if(lastName.charAt(0) <= 'M') {
    line = 1;
} else {
    line = 2
}

The complete code looks like:

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;
// here's your code
    if(lastName.charAt(0) <= 'M') {
        line = 1;
    } else {
        line = 2;
    }
    return line;
  }

}

That should do it for you.

Steve.

Great explanation thanks

No problem. As long as you got it sorted. :-)

Steve thanks for great explanation! Tumbs up!