Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Delivering the MVP Validation

Mallikarjuna A S
seal-mask
.a{fill-rule:evenodd;}techdegree
Mallikarjuna A S
Python Web Development Techdegree Student 7,017 Points

Validation 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Mallikarjuna A S
Python Web Development Techdegree Student 7,017 Points

I 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; }

Your 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
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.

You are welcome John Paige, hope you have solved your problem.