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 trialAlexander Nowak
498 PointsComparing characters challenge
Hi, I am having trouble sorting the people into two lines by comparing there last name to then determine which line they go in.
Thanks!
public class ConferenceRegistrationAssistant {
public int getLineFor(String lastName) {
lastName = console.readLine("Enter your last name")
char firstLetter = lastName.charAt(0);
if (firstLetter) {
/* 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;
}
}
1 Answer
Kevin Faust
15,353 PointsHey Alexander,
Firstly, we do not prompt for a lastname within the method. A lastname string will already be passed in for us. All we have to do is check if the letter is less than 'N'
int line=0
should be the first line in our method as everything we will do in the code will manipulate that number
we then do a simple if statement. you got the charAt(0) part correct. All you have to do is check if the charAt(0) is less than the letter 'N' aka letters 'A through M'. If it is indeed less than 'N' we put them in line 1. otherwise we put them in line 2. I have attached the solution below as reference in case your still stuck
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) < 'N') {
line = 1;
} else {
line = 2;
}
return line;
}
}
Happy coding and best of luck,
Kevin
Alexander Nowak
498 PointsAlexander Nowak
498 PointsHi Kevin, thanks again!!
what does int line = 0; mean/do?
Kevin Faust
15,353 PointsKevin Faust
15,353 PointsHey Alexander,
Its basically just a placeholder value. Its just an empty int variable and through our code we will either add 1 or 2 to it depending on the last nam and then we return that value.
Kevin