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

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,661 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".