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

/ConferenceRegistrationAssistant.java:13: error: unreachable statement char guest = lastName.charAt(0); ^

Can anyone help?

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 guest = lastName.charAt(0);  


    if(guest > 'A' && guest < 'M') {
      line =1;
      return line;
      } else {
      line =2;
      return line;
    }



  }

}

Thanks!

2 Answers

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

Hi, doc chatman , I was asked to answer this question, so I'll try my best.

there're couple problems with the code

  1. your code has 3 return statement, the last 2 look okay to me, there problem is the first return line;. You have to remember that return statement will terminate the function execution. That means as soon as the compiler hits the first return statement, it'll exit the function and whatever code you have after the first return statement don't get executed.
  2. Like George said, you also need to check for whether first char of lastName equals to A & M.
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;
    // deleted the first return statement


   char guest = lastName.charAt(0);  


    if(guest >= 'A' && guest <= 'M') { // equality check for 'A' & 'M'
      line =1;
      return line;
      } else {
      line =2;
      return line;
    }



  }

}

One last thing, I'm don't know if you've learned about ternary operator in Java during the course lecture; if you do, the code can be simplified a bit.

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 */
    return (lastName.charAt(0)>='A' && lastName.charAt(0)<='M') ? 1 : 2;
  }
}

Cheers.

Thanks!

George Pirchalaishvili
George Pirchalaishvili
3,747 Points

There are actually couple of things :)

  1. You have a return statement before you char guest = lastName.charAt(0); . This stops your method without going further
  2. In your if statement you have (guest>'A'), but what if guest = 'A'? :)

Hope this helps ;)