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

Lee Finch
PLUS
Lee Finch
Courses Plus Student 1,411 Points

I dont know how to do this

Im either getting an array needed but string found error when i use the index, or i get a bad use of symbol error when i do not use index

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[0] <= "M")
    {
      line = 1;
    }
    else
      line = 2;
    return line;
  }

}

2 Answers

Alistair Mackay
Alistair Mackay
7,812 Points

You're close but as Daniel pointed out above you're not checking the first letter of lastName correctly - change your quotes to single quotes and make use of the charAt() method to get the first letter of the lastName variable.

The below code worked for me, give it a try if you're really stuck:

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 the first char of lastName Less
    than/equal too 'A' && Greater than/
    equal too 'M' return line = 1; else line 2;*/
    if (lastName.charAt(0) >= 'A' && lastName.charAt(0) <= 'M') {
      line = 1;
    } else {
      line = 2;
    }
    return line;
  }

}

I hope the above helps.

daniel104729
daniel104729
7,176 Points

In Java strings are not character arrays. If you want to get a particular character from a String you call the charAt(int index) function. You see more details about Java String by looking at the docs provided by Oracle https://docs.oracle.com/javase/8/docs/api/java/lang/String.html. You may need to surround the M with single quotes so it will be evaluated as a char instead of a String.