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 trialAlbert Reel
5,555 PointsDelivering the MVP and stumped.
Can someone point me towards the source of my errors. I was trying to isolate the target char from the beginning of the input string and I keep running into syntax errors.
Anyone have a fresh look at this?
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
char mFirstLetter = valueOf(fieldName.CharAt(0));
char mSecondLetter = valueOf(fieldName.CharAt(1));
while (mFirstLetter != 'm') {
throw new IllegalArguementException ("Field must start with m");
}
while (! mSecondLetter.isUppercase) {
throw new IllegalArguementException ("Please use CamelCase");
}
return fieldName;
}
}
2 Answers
Stephen Bone
12,359 PointsHi Albert
There were a few issues mainly syntax errors. I've updated your code to reflect the changes and annotated it as best as I can remember ;)
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
// removed the valueOf method as it was unnecessary
// should be a lowercase 'c' for charAt method
char mFirstLetter = fieldName.charAt(0);
char mSecondLetter = fieldName.charAt(1);
while (mFirstLetter != 'm') {
// removed an 'e' in the spelling of argument
throw new IllegalArgumentException ("Field must start with m");
}
// need to call the isUpperCase static method directly from the Character class
// missing a capital 'C' in isUpperCase method
while (! Character.isUpperCase(mSecondLetter)) {
throw new IllegalArgumentException ("Please use CamelCase");
}
return fieldName;
}
}
Hope it helps!
Stephen
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"
public class TeacherAssistant {
public static String validatedFieldName(String fieldName) {
if (fieldName.charAt(0) != 'm' || !Character.isUpperCase(fieldName.charAt(1))) {
throw new IllegalArgumentException("The field name does not match the style");
}
return fieldName;
}
}
Albert Reel
5,555 PointsAlbert Reel
5,555 PointsIm embarrassed how many times I read over those mistakes. This makes a lot more sense. Thank you.
Stephen Bone
12,359 PointsStephen Bone
12,359 PointsNo worries, we all experience moments when we just can't see the error! Glad I could help.