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 trialdlpuxdzztg
8,243 Points[HELP] I'm close...but not there yet...
When I run this piece of code, it doesn't give me any errors...except for one:
"I entered Anderson and expected to get back line 1 as A comes before 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 */
if (lastName.charAt(0) >= 'M') {
int line = 2;
} else {
int line = 1;
}
int line = 0;
return line;
}
}
2 Answers
Anjali Pasupathy
28,883 PointsYou've nearly got it! You just need to change where you declare line. You need to declare line before the if statement, and get rid of the declaration of line after the if statement. Also, because you declare line before the if statement, you don't need to declare it in any of the blocks - you just need to write "line = 2" instead of "int line = 2", or "line = 1" instead of "int line = 1".
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;
if (lastName.charAt(0) >= 'M') {
line = 2;
} else {
line = 1;
}
return line;
}
I hope this helps!
Jacob Martin
4,182 PointsOk I figured it out. Looking at your if/else statement everything is correct, but looking afterwords you are declaring int line to be 0. So no matter what they put in your output of line will be 0.
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 = 2;
} else {
line = 1;
}
return line;
}
}
A good way to check this for future reference is to use a printf or System.out.println(line) to see what the program is outputting.
dlpuxdzztg
8,243 PointsIt's giving me a compile error now
./ConferenceRegistrationAssistant.java:8: error: variable line is already defined in method getLineFor(String)
int line = 1;
^
./ConferenceRegistrationAssistant.java:10: error: variable line is already defined in method getLineFor(String)
int line = 2;
^
2 errors
dlpuxdzztg
8,243 Pointsdlpuxdzztg
8,243 PointsAh, that makes a lot of sense!
Thank you!
Anjali Pasupathy
28,883 PointsAnjali Pasupathy
28,883 PointsYou're welcome!