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 trialMallikarjuna A S
Python Web Development Techdegree Student 7,017 PointsValidation Objective Test
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
// Convert String to Character of Array
char[] charArray = fieldName.toCharArray();
//Compare two Character where if value matches it return 0 or value charArray[0] is less than m then it returns -1 and charArray[0] is greater than m it returns 1
int firstField = Character.valueOf(charArray[0]).compareTo('m');
//First Letter Validation
if ( firstField != 0) {
throw new IllegalArgumentException("Invalid Argument");
}
//Second Letter Validation
if (Character.isLowerCase(charArray[1])){
throw new IllegalArgumentException("Invalid Argument");
}
return fieldName;
}
}
When I try to Check the Work to Pass the Code Challenge " I am getting Following Error "m_first_name has to failed but it is passed"
3 Answers
Mallikarjuna A S
Python Web Development Techdegree Student 7,017 PointsI have solved it
char[] charArray = fieldName.toCharArray();
//Compare two Character where if value matches it return 0 or value charArray[0] is less than m then it returns -1 and charArray[0] is greater than m it returns 1
char letter1 = charArray[0];
char letter2 = charArray[1];
//First Letter Validation
if ( firstField.indexOf(letter1) != 0) {
throw new IllegalArgumentException("Invalid Argument");
}
//Second Letter Validation
if (!Character.isUpperCase(letter2)){
throw new IllegalArgumentException("Invalid Argument");
}
return fieldName; }
Soumyadeep Mukhopadhyay
1,772 PointsYour code is perfect except a small mistake i.e., fieldName instead of firstField
char[] charArray = fieldName.toCharArray();
//Compare two Character where if value matches it return 0 or value charArray[0] is less than m then it returns -1 and charArray[0] is greater than m it returns 1
char letter1 = charArray[0];
char letter2 = charArray[1];
//First Letter Validation
if ( fieldName.indexOf(letter1) != 0) {
throw new IllegalArgumentException("Invalid Argument");
}
//Second Letter Validation
if (!Character.isUpperCase(letter2)){
throw new IllegalArgumentException("Invalid Argument");
}
return fieldName; }```
John Paige
7,436 Points 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[] charArray = fieldName.toCharArray();
char letter1 = charArray[1];
if (fieldName.charAt(0) != 'm' || !Character.isUpperCase(letter1)) {
throw new IllegalArgumentException("Invalid fieldName");
}
return fieldName;
}
}
The passing code I came up with. I did learn the "char[] charArray" syntax from this thread. Thank you, Mallikarjuna A S and Soumyadeep Mukhopadhyay for the contributions. The part I got stuck on was figuring out how to isolate the first character (after 'm') for an upper/lowercase comparison.
Soumyadeep Mukhopadhyay
1,772 PointsYou are welcome John Paige, hope you have solved your problem.