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

comparing characters

Hi guys am trying to run this code but its giving me a bummer and unable to show syntax errors where am i wrong

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;
    return line;
  }
char letter = lastName.charAt(0);
    if(letter > 'M'){
      line = 1;
    } 
    if('M' < letter){
     line = 2; 
    }
    return line;
  }

}

1 Answer

Stephen Bone
Stephen Bone
12,359 Points

Hi MUZ140151 Chapara

You're close with your answer but you first need to move it inside of the getLineFor method.

As in:

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;
    char letter = lastName.charAt(0);
    if(letter > 'M'){
      line = 1;
    } 
    if('M' < letter){
     line = 2; 
    }
    return line;
  }

Next you need to look a bit more closely at your if statement logic as it's not quite right and replace them with an if else statement otherwise once you get your if statement logic correct you'll actually receive a message stating your code is correct but change it anyway! :)

Hope it helps but if you need any further assistance let me know.

Stephen

Matthew Stanciu
Matthew Stanciu
4,222 Points

I'm having the same problem. I checked my code with yours and it seems pretty much the same, apart from a few spaces in the brackets and a different if statement. I also added an else statement.

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;
    char letter = lastName.charAt(0);
    if(letter <= 'M') {
      line = 1;
    } else (letter >= 'M') {
      line = 2;
    }
    return line;
  }
}
Stephen Bone
Stephen Bone
12,359 Points

Hi Matthew

I think your problem is that char 'M' is going to be true in each case and therefore added to both lines 1 & 2.

This is because your first if statement says if letter is less than or equal to 'M' and the second statement says if letter is greater than or equal to 'M'.

I've made the small change mentioned to your code below:

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;
    char letter = lastName.charAt(0);
    if(letter <= 'M') {
        line = 1;
    }
    if(letter > 'M') {
        line = 2;
    }
    return line;
}

Once you've corrected the logic of your if statements however you will still receive a message asking you to change them to use the if else statement instead before the task will pass.

EDIT: OK you've changed and updated your comment while I was answering it in its original state :).

The same issue applies although you've now already changed it to an if else statement. However remember that you don't pass a condition to the else statement as this is an if all else fails do this statement.

Hope it helps!

Stephen

Matthew Stanciu
Matthew Stanciu
4,222 Points

Thanks so much for your help! Your solution worked! I was typing else without noticing that I should just be typing another if. Sometimes I do that and it takes me hours to notice.