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

Chris Ayers
Chris Ayers
554 Points

Validation Question

So I have this so far. I am not sure about the try catch, and also if I am using the .toUpperCase correctly.
Please give me input on how to make this work and look better!

Thanks

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
   boolean isValid = false;
    String name = fieldName;
    if (name.charAt(0) == 'm' && name.isUpperCase(name.charAt(1)){
        isValid = true;
    return fieldName;
        }
        else{
          isValid = false;
          return "invalid casing.";
        }
  }

}
Joakim Mattsson
Joakim Mattsson
3,074 Points

Hello Chris, Im just as new as you are with this so im not sure i can answer alot of question but I see a couple of small things in your code.

The first and I think the most important thing is that in your else you have to throw and IllegalArgumentException as the instructions were.

Also I think you have to change the if argue to exemple:

if(name.charAt(0) == 'm' && Character.isUpperCase(name.charAt(1)) == true){}

I tested your code with small changes and completed the exercise with this code:

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 name = fieldName;
    if (name.charAt(0) == 'm' && Character.isUpperCase(name.charAt(1)) == true){
      return fieldName;
    } else {
     throw new IllegalArgumentException("asd");
    }
  }
}

hope this help :)

1 Answer

Chris Ayers
Chris Ayers
554 Points

Thank you very much Joakim for the input and the help! I was actually a little confused on where to put the try and catch! In my programming class we had just started talking about try and catch at the end of the semester. We used j-unit to test everything, so this has been interesting to try and get used to. lol Thanks again for the help and this helps me out tremendously!