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

Pauliina K
Pauliina K
1,973 Points

Chars

Hey, again. I feel like I post things here all the time, but oh well, how else am I gonna learn? I got stuck again.

It says if the lastName is between A and M it's supposed to go into line 1, otherwise line 2. But I don't know how you write between A and M? 'A-M'? 'A' - 'M'? There's also a NEW INFO part in the description saying that you can compare chars with > and <, but I don't see how that has to do with this assignment? Unless that's how it's written. 'A<M' or 'A'<'M' or something. I feel like this wasn't even brought up in the video? Or did I just misunderstand something?

2 Answers

Hi Pauliina,

Yes, you can compare lots of things using greater than and less than symbols. For characters, it just enforces the alphabet rules.

So, let's assume everyone's name is spelled correctly and their name starts with a capital letter.

We want anyone who has a name starting with A to M (less than or equal to 'M') to be in line 1; everyone else is in line 2. The first character of a string can be isolated by using .charAt(0).

Putting that lot together gives us:

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

I hope that makes sense.

Steve.

Pauliina K
Pauliina K
1,973 Points

Oh okay, that does make sense. Thanks! One thing though, in the video he says that counting always starts with 0, so the first letter in a word is the 0th letter and the second letter is the 1st letter. Right? But then shouldn't line 1 be called line 0 and line 2 be line 1?

Hiya,

Glad that worked for you.

Yes, system stuff counts from zero in things like array elements etc. But for things that you define yourself you can call it whatever you want. A line isn't a system thing; it's user-generated. The two queues could equally be X & Y or Arrivals & Departures - so the system isn't counting them for us; we just chose to call them 1 & 2.

Hope that's clear!

Steve.

Pauliina K
Pauliina K
1,973 Points

Oh! That makes sense! Thanks for clearing that up!