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

Jordan Man
Jordan Man
2,736 Points

How do i do this challenge? i am completely lost.

Lines are determined by last name, and they will end up in line 1 or line 2. Please help me fix the getLineFor method.

NEW INFO: chars can be compared using the > and < symbols. For instance 'B' > 'A' and 'R' < 'Z'

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 */
     char line = lastName.charAt(0);
   return line;
   if (line >= m){
     System.out.println("line 1");
   } else {
     System.out.println("Line 2");
   }

  }

}

2 Answers

  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;  //initialize line

    if (lastName.charAt(0) < 'M') {  //get first letter and compare to 'M'
      line = 1;  //re-initialize line
    } else {
      line = 2;
    }
    return line;  
  }
}

Under the covers chars are ints (their values are their ASCII values) so they can be compared using comparison operators like <, >, etc.

Jordan Man
Jordan Man
2,736 Points

brilliant thank you for your help. and thank you for breaking it down so i would understand. i have just a follow up question about the final line "return line" what is that doing - with the introduction of the if statement isnt the only options to be put in either line 1 or line 2?

The int in public int getLineFor(...) is the return type, and what it's promising is that this function will return an int. In the if... else... statement you set line to either 1 or 2. And then, after the if... else... return line; returns the number to the calling program.

Jordan Man
Jordan Man
2,736 Points

perfect. thank you again!