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 trialSean Kelly
843 PointsTeacherAssistant exercise problem in Java Objects
"mFirstName should have passed, but "please make the second letter upper case!" was reported
Don't really understand where I have gone wrong!
public class TeacherAssistant {
public static String validatedFieldName(String fieldName) {
char firstLetter = fieldName.charAt(0);
firstLetter = Character.toLowerCase(firstLetter);
if(firstLetter != 'm') {
throw new IllegalArgumentException("Please begin member fields with 'm' "); }
char secondLetter = fieldName.charAt(1);
if(secondLetter != Character.toUpperCase(secondLetter)); {
throw new IllegalArgumentException("Please make the second letter upper case!"); }
// 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
}
}
2 Answers
Dan Johnson
40,533 PointsOn the line with your second conditional, you have a semicolon terminating the if statement before it enters the block:
// Remove the semicolon
if(secondLetter != Character.toUpperCase(secondLetter));
So your second exception gets thrown regardless of the outcome.
Don't forget to return fieldName at the end.
Jorge Paredes Cimadevilla
3,068 PointsHi Sean for the second question you asked, you need to add a third validation (they didn't mention this I know, quite frustrating when I found out).
You could do something like this in your case:
if (! Character.isLetter(secondLetter)) {
throw new IllegalArgumentException("A letter is required");
}
Sean Kelly
843 PointsSean Kelly
843 PointsThanks, Dan. I fixed the error on the second conditional, but I'm now getting another error message, this time on the first conditional:
" Expected "m_first_name" to fail but it passed "