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

Anusha Singh
PLUS
Anusha Singh
Courses Plus Student 22,106 Points

I did not understand what to do

The question is 'So I was wondering if you could help me with some of these code challenges. As you might've seen when a member field name doesn't match the style we've talked about, I send an error about it. Let's write some code to validate the style of the field name.

I've added the method validatedFieldName it will return the validated field name. If the value passed in doesn't meet the requirements, throw an IllegalArgumentException. I'll keep you posted on the results every time you press check work.' Could you help

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
    return fieldName;
  }

}

I was also facing a similar problem. Thanks for posting.

2 Answers

The correct version of the answer is here. This is running. https://teamtreehouse.com/community/validation-objective-test

Kristian Gausel
Kristian Gausel
14,649 Points
public class TeacherAssistant {

  public static String validatedFieldName(String fieldName) {
    // These things should be verified:
    // 1.  Member fields must start with an 'm'
    if(fieldName.toCharArray()[0] != 'm'){ throw new IllegalArgumentException();}

    // 2.  The second letter in the field name must be uppercased to ensure camel-casing
    if(fieldName.toUpperCase().toCharArray()[1] != fieldName.toCharArray()[1]){
      throw new IllegalArgumentException();
    }

    //For some reason they also want us to make sure we dont use underscores, which we do like this:
    if(fieldName.indexOf("_") != -1){
      throw new IllegalArgumentException();
    }

    // NOTE:  This is probably easier to do with regular expressions ...

    return fieldName;
  }

}

Not at all efficient, and you can also use substrings and so on. The best solution when we add complexity to our rules though is to use regular expressions.

My code was

public class TeacherAssistant {
 public static String validatedFieldName(String fieldName) {
String mFirstName = "text";
String mMicro = "pretext";
String mMacro = "context";
if (! mFirstName.equals(fieldName))
{
throw new IllegalArgumentException("Please enter a valid letter");
}
if ( mMacro.indexOf(fieldName) >= 0 || mMicro.indexOf(fieldName) >=  0)
{
throw new IllegalArgumentException("This word has been repeated");
}
return fieldName;
}```

The error i have encountered with after applying the code is :-
The variable to be passed is "mFirstName" and the reult should be "Please enter a valid letter".