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 trialNicholas Steele
683 Pointsstuck on java track task? not sure how to compare the last name to a character
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 {
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.substring(0,1) < 'M') {
int line = 1;
}
else {
int line = 2;
}
return line;
}
}
3 Answers
Andrew Winkler
37,739 PointsTry this out (I commented to explain the logic):
public class ConferenceRegistrationAssistant {
public int getLineFor(String lastName) {
int line = 0;
if(lastName.charAt(0) > 'M') {
/* The getLineFor method takes a String "lastName" as an argument.
Strings are concatenations of characters and they can be counted from 0 .
You can use the charAt() method to compare the char at the place 0 inside String "lastName"
with a single character of your interest.
Here we write a condition: if char at position 0 of the "lastName" is greater then character 'M',
the line is 2.
*/
return line = 2;
// return int 2
} else
// everething is under 'M'
// line is 1
return line = 1;
// return int 1
}
}
If this is helpful please make sure to vote best answer! Happy coding!
Plamen Neshkov
18,236 PointsYou need to compare the first character of the last name to the aforementioned intervals of characters:
lastName.charAt(0) >= 'A' && lastName.charAt(0) <= 'M'
The reason your code doesn't work is because the substring() method returns a String and not a char primitive type and you cannot compare incompatible types. The Java compiler simply doesn't know how to infer this type of comparison.
Jeff Wilton
16,646 PointsWhen you are working on these code challenges, you can hit the preview button to see any compiler messages, such as these in your case:
./ConferenceRegistrationAssistant.java:7: error: bad operand types for binary operator '<'
if(lastName.substring(0,1) < 'M') {
^
first type: String
second type: char
./ConferenceRegistrationAssistant.java:8: error: variable line is already defined in method getLineFor(String)
int line = 1;
^
./ConferenceRegistrationAssistant.java:11: error: variable line is already defined in method getLineFor(String)
int line = 2;
^
3 errors
The first error is telling you that you can't compare two different types, they must be the same type (either both Strings or both Chars). The substring method returns a string, and 'M' is a char. I would recommend using the charAt method instead of substring since you are only interested in a single char. The other two problems are telling you that you can't create new variables with the same name (int line), so remove the 'int' from the last two.
if(lastName.charAt(0) < 'M') {
line = 1;
}
...
Jeison Dias
3,031 PointsJeison Dias
3,031 PointsReally helpful! I wasn't understanding and your logic explanation opened my eyes!