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 trialCharles Werder
Courses Plus Student 742 PointsvalidatedFieldName
I have been looking at my code for a while, and I can't figure out where I went wrong.
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 IllegalArgumentException("There is an error");
}
if (!Character.isLetter(fieldName.charAt(1)) ||
fieldName.charAt(1) != Character.toUpperCase(fieldName.charAt(1))) {
throw IllegalArgumentException("There is an error");
}
return fieldName;
}
}
3 Answers
Charles Werder
Courses Plus Student 742 PointsOh, wow. Can't believe I forgot that. Thanks!
Stone Preston
42,016 Pointsyou are missing the new keyword when throwing the exception:
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("There is an error");
}
if (!Character.isLetter(fieldName.charAt(1)) ||
fieldName.charAt(1) != Character.toUpperCase(fieldName.charAt(1))) {
throw new IllegalArgumentException("There is an error");
}
return fieldName;
}
however, there is a Character class method called isUpperCase that greatly simplifies the second check:
if (!Character.isUpperCase(fieldName.charAt(1))) {
throw new IllegalArgumentException("There is an error");
}
see the documentation on the Character class for more helpful methods
Jess Sanders
12,086 Pointsprivate char validateGuess(char letter) {
if (!Character.isLetter(letter)) {
throw new IllegalArgumentException("A letter is required."
}
letter = Character.toLowerCase(letter);
if (mMisses.indexOf(letter) >= 0 || mHits.indexOf(letter) >= 0) {
throw new IllegalArgumentException(letter + " has already been guessed") }
return letter;
}
The above code is the example to follow.
In pseudocode, we want to check for the following condition, and throw an error: " if the character at index 0 of fieldName does not equal 'm' OR the character at index 1 of fieldName is not upper case"
Stone Preston
42,016 PointsStone Preston
42,016 Pointsno problem