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

how do i refer to the first letter of a string?

if i had a last name stevens (for example) how would i refer to the first s to compare it to another character?

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 */
    if (lastName
    int line = 0;
    return line;
  }

}

Hi Nicholas,

I think you might be looking for the substring() method. String firstLetter = lastName.substring(0, 1);

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Nicholas;

Welcome to Treehouse!

You could use the charAt() method which returns the character located at the String's specified index. The string indexes start from zero. So if you were to have:

String name = "Nicholas";
System.out.println(name.charAt(3));

We would get the letter h printed.

Post back with further questions.

Happy coding,
Ken