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

Eric Stermer
Eric Stermer
9,499 Points

Does using charAt() give you the char at the parameter location?

I used charAt() to locate the char at the location of 0 in the string lastName. I typed it out as lastName.charAt(0) and then just checked if that was greater than or equal to 0 and less than or equal to 12 since A is 0 and M is 12 and then returned the line at the value I want such as line 1 and then just used the else to say anything other than the condition above will return the line of 2. I am not fully understanding why the test is not 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 */
    int line = 0;

    if(lastName.charAt(0) >= 0 && lastName.charAt(0) <= 12){
      return line += 1;
    }else{
      return line += 2;
    }

  }

}

2 Answers

Hi Eric,

Yes, charAt(0) gives you the first letter of a string. However, they aren't numbered as you suspect.

A is less than M, but you can test them directly, not with numbers. Have a look at this post for detailed explanation, but the code looks like this:

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

You can have two return statements like you have done; that's fine. I'd have to check whether return line += 1 does the returning first, then the increment, though. I think you're safe; but that might just return line when it is zero, then do the += bit.

Steve.

+= works just fine. :-)

Kevin Faust
Kevin Faust
15,353 Points

im not sure what you did to be honest. using charAt returns a character and why are you comparing a character to a number? This was stated in the instructions:

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

All you need is a single argument inside your if statement. If the character at the first index is 0 (you did that part right) is less than or equal to the letter 'm', then we can increase the line by 1. otherwise (meaning the letter is greater than 'm'), we increase the line by 2. Does that make sense?

i attached the answer below for reference

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

}
Eric Stermer
Eric Stermer
9,499 Points

thanks for the help, before I saw your comment I realized what I was doing, so I did pretty much what you did comparing the charAt(0) to 'M'.

Thanks!