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 trialMichael LaCroix
5,828 PointsCan't figure out how to verify capitalization
I'm stuck on how to verify if the second letter of the member variable is capitalized. I've tried a number of things.
fieldName.isUpperCase != charAt(1) fieldName.charAt(1) != isUpperCase !fieldName.isUppercase(charAt(1))
None of these worked and I'm stuck. Would love a thought on why any or all of the above didn't work. Thanks.
public class TeacherAssistant {
public static String validatedFieldName(String fieldName) {
if (fieldName.charAt(0) != 'm' || fieldName.charAt(1) != isUpperCase) {
throw new IllegalArgumentException("Field must begin with 'm' and second letter must be capitalized.");
}
// These things should be verified:
// 1. Member fields must start with an 'm'
// 2. The second letter in the field name must be uppercased to ensure camel-casing
// NOTE: To check if something is not equal use the != symbol. eg: 3 != 4
return fieldName;
}
}
5 Answers
Stephen Bone
12,359 PointsHi Michael
I don't know if you've managed to figure this out or not but to build on what Logan has suggested.
isUpperCase and isLowerCase are static methods which means you must call them directly from the class. In this case these methods are called from the Character class, like so Character.isUpperCase(). Then you need to pass in the Char you want to test which we can do using the charAt() method of the fieldName String as we've been doing previously.
The code will look something like below:
Remember if we test for upper case we may need to specify a '!' to change the statement to not upper case
if (fieldName.charAt(0) != 'm' || ! Character.isUpperCase(fieldName.charAt(1))) {
Hope it helps!
Stephen
Logan R
22,989 PointsYou can use the Character class.
Character.isUppercase(CHAR);
Michael LaCroix
5,828 PointsThanks, but I know to use the character class. That's exactly what I'm already trying to do. I can't figure out how to single out the second variable.
"2. The second letter in the field name must be uppercased to ensure camel-casing"
The closet I think I came was this: fieldName.charAt(1) != isUpperCase but that didn't work.
Michael LaCroix
5,828 PointsAlso tried fieldName.isLowerCase(charAt(1)) and that didn't work either.
Michael LaCroix
5,828 PointsThanks Steve. I reposted and got almost the exact advice you gave.