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

Aaron Coursolle
Aaron Coursolle
18,014 Points

[Solved]Validation challenge?

I don't know what to put in my "if" condition to make the code work.

The comments that were included in the challenge itself are more or less the instructions for the challenge...you have to throw an IllegalArgumentException if the conditions do not pass.

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' && (fieldName.charAt(1) > "A" && fieldName.charAt(1) < "Z"))
  {
     throw new IllegalArgumentException("The " + fieldName + " style does not match.");
   }
    return fieldName;
  }

}

2 Answers

Hi Aaron,

In your case it's best to use Character.isUpperCase()

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' && Character.isUpperCase(fieldName.charAt(1))))
  {
     throw new IllegalArgumentException("The " + fieldName + " style does not match.");
   }
    return fieldName;
  }

}

Best Regards,

Philip

Aaron Coursolle
Aaron Coursolle
18,014 Points

Hi Philip,

Won't the m_somethingPluggedInHere still pass the code you posted above?

I don't think so https://ideone.com/xrKgpH

Aaron Coursolle
Aaron Coursolle
18,014 Points

I do like the elegance of the not operator that you used at the start of the condition statement, instead of making everything "not" one condition at a time

Aaron Coursolle
Aaron Coursolle
18,014 Points

Okay. I searched the forum, but everyone has their own way of coming up with the same answer. Here is how I got my way to work (mainly by changing quotes from double to single and changing the && to ||):

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' || (fieldName.charAt(1) < 'A' || fieldName.charAt(1) > 'Z'))
  {
     throw new IllegalArgumentException("The " + fieldName + " style does not match.");
   }
    return fieldName;
  }

}