Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Creating the MVP Comparing Characters

Creating the MVP; returning 1 or 2 according to the last name. Is it correct? or I have a bug?

I am trying this on the java-repl it works, but when I try on the screen I get a runtime error. What should be the problem?

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.indexOf(0) >= 'A' && lastName.indexOf(0) <= 'M'){
      line = 1;
      //return line;
    }
    else{
      line = 2;
       //return line;
    }

    return line;
  }

}

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,865 Points

Hi, Botan Abdullah , you got it mostly correct, only thing you should change is to replace your indexOf method with charAt, then your code will work perfectly.

thanks a lot , that worked, however shouldn't this ".indexOf()" method work as well? and do the same job?

William Li
William Li
Courses Plus Student 26,865 Points

Hi, Botan.

No, indexOf and charAt are 2 Java String methods that do almost the exact opposite thing.

  • indexOf method, it taks an char as argument, and return the index (as an int) of the first occurrence of that character.
  • charAt method, takes an int as argument, and return the character at that particular index.

Now, you can see, charAt is what you wanna use at this case.

Now I understand the difference, thanks.