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

Neil Gordon
Neil Gordon
8,823 Points

Teacher Assistant Help for challenge. I think I am close but I am not seeing why i have 4 errors

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
   String  mTeacherassistant = fieldName; 
    //member fields start with letter 'm'  
    Boolean mFieldLetterM = fieldName.startsWith('m');
    //true or false for an uppercase letter second position
    boolean mFieldUpperCase = Character.isUpperCase(fieldName.charAt(1)); 

    if(!mFieldLetterM || !mFieldUpperCase) { 

    throw new IllegalArgumentException("field must start with an'm' and CamelCasing"); 

    }


    }

    return fieldName;
  }

}

4 Answers

Kevin Faust
Kevin Faust
15,353 Points

Hey Neil

public class TeacherAssistant {

  public static String validatedFieldName(String fieldName) {
   // you never used this: String  mTeacherassistant = fieldName; 

    Boolean mFieldLetterM = fieldName.charAt(0) == 'm';

    boolean mFieldUpperCase = Character.isUpperCase(fieldName.charAt(1)); 

    if(mFieldLetterM && mFieldUpperCase) { 

    return fieldName;

    }


     // unnecessary bracket here }


    throw new IllegalArgumentException("field must start with an'm' and CamelCasing"); 
  }

}

First off you need to use the charAt() method to check if the 'm' is first. startsWith() deals with strings but we are just concerned with a char

Instead of || we want to use && because if both conditions are not true, then we want to throw the error.

You also had an unecessary bracket near the end

You also created a variable but never used it. Just wanted to point that out

And lastly in the code I posted above, I modified it a little bit because the code checker wasn't allowing me to pass. not sure why.

But anyways if you have any other questions feel free to let me know,

Kevin

Neil Gordon
Neil Gordon
8,823 Points

i figured it out my language was off

Neil Gordon
Neil Gordon
8,823 Points

thank you your version is a lot cleaner

Kevin Faust
Kevin Faust
15,353 Points

Glad it made sense. Here is a version with even less code:

public class TeacherAssistant {

  public static String validatedFieldName(String fieldName) {

    if (fieldName.charAt(0) == 'm' && Character.isUpperCase(fieldName.charAt(1))) {
     return fieldName;
    }

    throw new IllegalArgumentException("ERROR"); 
  }

}

Instead of creating variables, we can just directly put our conditions in our if statement.

Neil Gordon
Neil Gordon
8,823 Points

that makes sense, less code is better thank you