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 trial

Java Java Objects (Retired) Delivering the MVP Validation

Problem with challenge Task in Java Ovject

This is a exercise: 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

But it says "m_first_name" passed. How does this come?

TeacherAssistant.java
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 new IllegalArgumentException("Start with m");
    }
    else if (fieldName.charAt(1) != fieldName.toUpperCase().charAt(1) && !Character.isLetter(fieldName.charAt(1))) {
      throw new IllegalArgumentException("Camel!");
    }
    else {
      return fieldName;
    }
  }

}

1 Answer

Nico Julian
Nico Julian
23,657 Points

Hi there,

In your second condition, you could use the isUpperCase method to determine if it's camel cased. I think you're getting the error "m_first_name passed" because the condition:

(fieldName.charAt(1) != fieldName.toUpperCase().charAt(1) && !Character.isLetter(fieldName.charAt(1))),

is checking first to see if the second letter doesn't equal its uppercased version, and simultaneously is not a letter. The '_' isn't a letter, but it appears to pass the first part of the condition, allowing it to slip through.

public class TeacherAssistant {

  public static String validatedFieldName(String fieldName) {

    if (fieldName.charAt(0) != 'm') {
      throw new IllegalArgumentException("Start with m");
    }
    else if (!Character.isUpperCase(fieldName.charAt(1)) {
      throw new IllegalArgumentException("Camel!");
    }
    return fieldName;
  }

}

Thanks! Your solution is so great!