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 trialdoc chatman
Courses Plus Student 602 Points/ConferenceRegistrationAssistant.java:13: error: unreachable statement char guest = lastName.charAt(0); ^
Can anyone help?
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;
}
}
}
2 Answers
William Li
Courses Plus Student 26,868 PointsHi, doc chatman , I was asked to answer this question, so I'll try my best.
there're couple problems with the code
- your code has 3
return
statement, the last 2 look okay to me, there problem is the firstreturn line;
. You have to remember that return statement will terminate the function execution. That means as soon as the compiler hits the firstreturn
statement, it'll exit the function and whatever code you have after the firstreturn
statement don't get executed. - 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.
doc chatman
Courses Plus Student 602 PointsThanks!
George Pirchalaishvili
3,747 PointsThere are actually couple of things :)
- You have a return statement before you char guest = lastName.charAt(0); . This stops your method without going further
- In your if statement you have (guest>'A'), but what if guest = 'A'? :)
Hope this helps ;)
doc chatman
Courses Plus Student 602 Pointsdoc chatman
Courses Plus Student 602 PointsThanks!