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

kai Detmers
kai Detmers
614 Points

Hey there, I really can't solve this task. I just want to store the first letter of the name and then compare it.

If it is bigger then R, it should return line 2, if not, then it returns line 1.

Thank you !

ConferenceRegistrationAssistant.java
import java.io.Console;
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 */
    Console console = System.console();
    String name = console.readLine("Enter your last name.");
    char nachname = name.charAt(0);
    if (nachname > 'R') {
    int line = 0;
      return line;}
    else {
      int line =1;
      return line;}

  }

}
Demauri Portis
Demauri Portis
4,809 Points

Well if its supposed to return line = 2; then you should change that 0 to a 2. Seems like the code works except for the fact you have a 0 line :D

ohhhhhh realized you passed in a name. so all of your console code & nachname is unnecessary, I would go with the code Michael Hess provided.

2 Answers

Michael Hess
Michael Hess
24,512 Points

Hi Kai,

You may want to use charAt(0) -- this will get the first letter of the last name. You can use the < operator to compare the first letter of the last name this way.

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.charAt(0) < 'M' ){
      line = 1;
    } else{
      line =2;
    }

    return line;
  }

}

Hope this helps! If you're still having issues hit me up!

kai Detmers
kai Detmers
614 Points

Hey :) Thanks for the help. That solved it. But i don't really unterstand why my method wasn't working. I did he same, but made a new variable "nachname".

Michael Hess
Michael Hess
24,512 Points

The nachname variable is not the only difference:

  1. You don't need to use the console to get the persons last name in the getLinefor method. The getForLine method's job is to send a person to line 1 if there last name is between A and Z and if it isn't send them to line 2. Console input is handled in the main method.

  2. In your code, the if statement will send someone to line 0 if there last name begins with a letter greater than 'R'. If last name is not greater than 'R' -- they will be sent to line 1. Whereas, we want to send a person to line1 if there last name begins with a letter A - M and line2 if the last name begins with a letter N - Z. It's not an off by one like with elements in an array -- 1 means 1 and 2 means 2 when using variables.

  3. The return statements in the if-else statement cannot be accessed outside of the if-else statement in the getLineFor method. So, the method doesn't have a return line statement -- which will cause an error.

If I need to clarify anything -- don't hesitate to ask!