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 trialahmed alghafli
3,446 PointsI have modeled an assistant for a tech conference. Since there are so many attendees, registration is broken up into two
I don't know what to do with this question pleas help me with it ?!!
I have modeled an assistant for a tech conference. Since there are so many attendees, registration is broken up into two lines. Lines are determined by last name, and they will end up in line 1 or line 2. Please help me fix the getLineFor method.
NEW INFO: chars can be compared using the > and < symbols. For instance 'B' > 'A' and 'R' < 'Z'
public class ConferenceRegistrationAssistant {
private String line 1;
private String line 2;
public conferenceRegistrationAssistant () {
line 1 = "";
line 2 = "";
}
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('A'<'B')) {
line 1 += lastname;
return line 1;
}else {
return line 2;
}
int line = 0;
return line;
}
}
5 Answers
Rebecca Bompiani
16,996 PointsHi Ahmed-
What they're asking for is this logic: If the first letter of lastName comes before (is less than) N, set the line to 1. Otherwise set the line to two.
In Code:
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 */
//create a line variable
int line = 0;
//if the first letter of the last name comes before N...
if (lastName.charAt(0)<'N') {
//set the line to 1
line=1;
}
//otherwise...
else {
//set the line to 2
line=2;
}
//return the line the person belongs in
return line;
}
}
Saad Khan Malik
25,199 PointsThis Works :)
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.indexAt(0);
if(letter < 'M'){
line = 1;
} else {
line = 2;
}
return line;
}
}
Saad Ahmed
7,090 PointsSalam Ahmed
The first thing you want to do in your logic is to find the first character of the name. (e.g. lastName.charAt(0) // this will only get the first letter of the last name variable (remember that array starts at 0 and not 1.)
then you want to compare the value of the first letter of the name to a fixed value (in your case, its the character 'N'). Then if it is greater than 'N' assign to line 2 and if not then assign to line one (or vice versa).
Maybe the model will help
char variable = lastName.charAt(0);
if(variable < 'N') { return lineOne; } else { return lineTwo; }
P.S. you will have to initialize the lineOne and lineTwo variables.
Hope this helps
Otis Mason
794 Pointsthanks alot
ahmed alghafli
3,446 Pointsthanks it helped now I kind of understand it ,
tiago goncalves
1,912 Pointstiago goncalves
1,912 Pointsreally well explained!!
Bradley Maravalli
8,927 PointsBradley Maravalli
8,927 PointsThanks for the explanation! As of May 2016, this is the right answer.
Bradley Maravalli
8,927 PointsBradley Maravalli
8,927 PointsThanks for the explanation! As of May 2016, this is the right answer.
Bradley Maravalli
8,927 PointsBradley Maravalli
8,927 PointsThanks for the explanation! As of May 2016, this is the right answer.