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

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,868 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,868 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.