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 trialAbhishek Gangadhar
9,467 PointsIsn't this valid?? I need to throw an exception if the second character is not in uppercase
I have used the isLowerCase() method to check if the character at index 1 i.e. the second character is in lower case or not. i'm getting an error "cannot identify Symbol"
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'){
throw new IllegalArgumentException("The first letter must be m");
}
if(isLowerCase(fieldName.charAt(1)))
throw new IllegalArgumentException("Following m must be a Upper Case Letter");
return fieldName;
}
}
2 Answers
Kevin Faust
15,353 PointsHey Abhishek,
Your really really close. Just 2 minor things:
fieldName.indexOf(0)!='m'
We're actually supposed to use charAt(0)
because we are checking the position of a char. indexOf() is when you pass in a char and it returns the index value of that char. However we are doing the opposite so we use charAt.
Secondly, it should be Character.isLowerCase() because that method is part of the Character class and we must tell the compiler that. Otherwise it will be confused
I hope this helped and you learned something new :)
Dont forget to mark as best answer so others with the same question can see
Happy coding and best of luck,
Kevin
Cameron Vinyard
5,899 PointsThere's another problem, if the second character is a symbol. To fix this, you must test the character using
"Character.isLetter(charAt(0));"
You can add this on to the isLowerCase test, using an "or" statement (||)
Abhishek Gangadhar
9,467 PointsAbhishek Gangadhar
9,467 PointsThank you Kevin :)