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 trialAli Abdulla
Courses Plus Student 979 PointsI can't solve this challenge!
I tried to solve this challenge but maybe it's hard Or I didn't catch it Can you help, please
public class TeacherAssistant {
public static String validatedFieldName(String fieldName) {
// 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
if(fieldName.indexOf(0) != 'm' && !Character.isLowerCase(fieldName.indexOf(1))){
throw new IllegalArgumentException("doesn't meet the requirements");
}
return fieldName;
}
}
2 Answers
Evan Demaris
64,262 PointsHi Ali,
There's actually a few issues here;
Your if
uses indexOf
instead of charAt
, so you're asking if index 'm' is not 0, instead of asking if index 0 is not 'm'. Similar problem for your case check.
You're also checking if the character is not lowercase, which doesn't necessarily mean that it's uppercase - it could be a symbol, for example. Even then, if you're throwing an exception for not lowercase, you're doing the opposite of what you want, because this statement needs to evaluate to true for the exception to be thrown. So you either want to reword your if
so that it defaults to returning and throws the exception otherwise, or check for not uppercase instead.
Finally, you're checking if both conditions are not true, when either one should throw an exception. So you'll want to use ||
instead of &&
- unless you choose to reword and default to returning the fieldName, in which case you'd need to remove the not
from both evaluations and keep the &&
.
Hope that helps!
public class TeacherAssistant {
public static String validatedFieldName(String fieldName) {
if(fieldName.indexOf('m') != 0 || !Character.isUpperCase(fieldName.charAt(1))){
throw new IllegalArgumentException();
}
return fieldName;
}
}
Ali Abdulla
Courses Plus Student 979 PointsThank you so much :)