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 Challenge: fix the getLineFor method

Challenge Task 1 of 1 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.

Please help fix the getLineFor method.

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

Question: Are you supposed to use charAt(0) to find (and test against) the first letter of lastName? (it doesn't say that, though, in the question/challenge...)

//the code initally given for the challenge
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;
  }

}


//Neither of these addon code snippets is passing:

    //attempt1
    if lastName.charAt(0) < 'N' {
      line = 1
    } else {
      line = 2
    }


    //attempt2
    if lastName.charAt(0) > 'M' {
      line = 2
    } else {
      line = 1
    }

I'm totally confused...this question is nothing like what was shown in the videos..

John Bergandino
John Bergandino
8,455 Points

You do in fact need to use charAt() to get the first letter of lastName. On your "if" statement make sure to use parenthesis around your condition and make sure to include semi-colons properly throughout your code....if you are not sure exactly how to use semi-colons you can do some research online (they are pretty much like a period at the end of a sentence). Also, place the code in-between the defined int (line) and where the line number is returned...the code runs from top to bottom so we need to make sure the line # is returned at the bottom (after our if statement is finished).

One last thing, we need to use "greater than or equal to" or "less than or equal to" rather than just "greater/less than" in this scenario. If you kept it as you had it above, all last names starting with letters 'M' or 'N' would not be provided a line #.

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;

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

      line = 1;

    } else {

      line = 2;

    }

    return line;
  }

}

2 Answers

Thank you both Craig and John!

I wish I knew how to mark John's response as "Best Answer" (with the green check mark). I'm seeing a "Best Answer" link under Craig's post, but not under John's)

John, you're right when you said: "On your 'if' statement make sure to use parenthesis around your condition and make sure to include semi-colons properly throughout your code..."

I'm still trying to get my head around the Java syntax...it's very unforgiving to even minor omissions (I'm finding out).

It was using

<= 

//instead of just 

<

//though, that I hadn't even considered.

However, I still I really wish (in possible future revisions of this question) it had actually explicitly said "Using charAt()..please do such and such...".

Right now I'm still struggling with the final 4 "wrapup" challenges for Java Objects. Hopefully I'm not alone, but if no one else posts about any of these "process of building a Forum" challenge exercises I may have more questions to post later.

Have a good day, forum people.

Jack Cummins
Jack Cummins
17,417 Points

Did you just call me a forum person?

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi James,

John is right. Your code actually is correct, I was checking that you understood the charAt, if and else logic. With proper syntax both of those statements should pass.

Let me know if it doesn't!