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 trialKenneth Balajadia
2,009 PointsSo I was wondering if you could help me with some of these code challenges. As you might've seen when a member field nam
So I was wondering if you could help me with some of these code challenges. As you might've seen when a member field name doesn't match the style we've talked about, I send an error about it. Let's write some code to validate the style of the field name.
I've added the method validatedFieldName it will return the validated field name. If the value passed in doesn't meet the requirements, throw an IllegalArgumentException. I'll keep you posted on the results every time you press check work.
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 (
return fieldName;
}
}
3 Answers
Andrew Winkler
37,739 PointsCan you elaborate what you're confused about or what you're struggling with?
public class TeacherAssistant {
public static String validatedFieldName(String fieldName) {
// These things should be verified:
// NOTE: To check if something is not equal use the != symbol. eg: 3 != 4
// 1. Member fields must start with an 'm'
if(fieldName.charAt(0) != 'm' ){
throw new IllegalArgumentException("Error!");
}
// 2. The second letter in the field name must be uppercased to ensure camel-casing
if(!Character.isLetter(fieldName.charAt(1)) || fieldName.charAt(1) != Character.toUpperCase(fieldName.charAt(1))){
throw new IllegalArgumentException("Error!");
}
return fieldName;
}
}
Kenneth Balajadia
2,009 PointsThnaks for the help andrew and grigorij! :)
Grigorij Schleifer
10,365 PointsHey Kenneth,
you are welcome
Grigorij Schleifer
10,365 PointsHi Kenneth,
in this challenge we will need to validate the first and the second character of the argument that the validatedFieldName() method is expecting. The argument is a String and its name is fieldName.
public class TeacherAssistant {
public static String validatedFieldName(String fieldName) {
// first character of the argument must start with 'm'
// like mArgumentString f.e.
// because member fields start with an `m`
if (fieldName.charAt(0) != 'm') {
throw new IllegalArgumentException("your exception text goes here");
}
// the second character must be uppercased
if (!Character.isUpperCase(fieldName.charAt(1))) {
throw new IllegalArgumentException("your exception text goes here");
}
return fieldName;
}
}
Because Strings are made of a zero-based concatenation of characters we can reference the first character as element 0 and second as element 1.
To be able to use the isUpperCased method we need to write the class name where this method is located. This class is called Character and has a static method isUpperCased().
I hope I could help a little bit.
Let us know if you need more help.
Grigorij