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 trialHank Tran
652 PointsHelp me.
validation
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.charAt(0) != 'm') {
throw new IllegalArgumentException("Make sure field name begins with n");
}
if(2. The second letter in the field name must be uppercased to ensure camel-casing) {
throw new IllegalArgumentException("The second letter in the field name must be uppercased.");
}
return fieldName;
}
}
2 Answers
Grigorij Schleifer
10,365 PointsHi Hank,
you should delete this line of code:
2. The second letter in the field name must be uppercased to ensure camel-casing inside of the second if-statement.
It is better to use a Character class for your if-statement, because this class provides methods to proof whether a char in a String is upper- or lowercased. The String class does not, so you canΒ΄t write
if (! fieldname.isUpperCase(charAt(1)){
throw new IllegalArgumentException("The second letter in the field name must be uppercased.");
}
bacause String "fieldName" doesnt contain the method like isUpperCase() this code would fail The Character class contains the method...
Look here :
http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html
So you should use a statement like this:
if(! Character.isUpperCase(fieldName.charAt(1))){
throw new IllegalArgumentException("the second letter in the field name must be uppercased");
}
return fieldName;
}
The Character contains a method to proof whether a String at char 1 (second char in the fieldName String) is uppercased or not. If the result is true (it is not - the compiler throws the exception, if false (second char is uppercased) the method returns the fieldName.
I hope I could explain it well, because english is not my best language :)
Best wishes and stay passionate for java ...
Seth Kroger
56,413 PointsIt looks like you copy-pasted the comment into the 2nd if statement, which is not a valid conditional statement. If you replace it with a conditional to check if the 2nd letter is uppercase, it should work fine.