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!

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

Why using negative or "not equal to symbol" (!) WON'T work in this.

Instead of the if-else code code why can't I use negation in 'if' and make it work, like:

if (fieldName.charAt(0) != 'm' && !Character.isUpperCase(fieldName.charAt(1))){
       throw new IllegalArgumentException(" It doesn't meet requirements."); 

    return fieldName;

The above does not work I have to use the one below

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))){
    else {
        throw new IllegalArgumentException(" It doesn't meet requirements."); 

    return fieldName;



3 Answers

What could be the reason for it not giving the desired results? It looks correct logically??

Yeah I believe you are correct, there is no output to go by but it does logically make sense that it would have the same output as the original. I was thinking about it wrong at first. I still believe the reason the quiz doesn't accept it is because they are strict in the code they are looking for.

Also, the return statement being outside of the if block means it could possibly execute if the if statement doesn't hit the error for some reason but it isn't the return you are looking for. If that makes sense. It is kind of poor design, I don't see a way an error could get by it but that sort of thing happens unexpectedly in code all the time.

I'm not sure I understand what your question is (what does your title mean?), but I can tell you that your return statement is in the wrong place. It should be after the if statement, so if the if statement is true it returns the result - if it is false then it throws an error.

if (fieldName.charAt(0) == 'm' && Character.isUpperCase(fieldName.charAt(1))) {
    return fieldName;
} else {
    throw new IllegalArgumentException(" It doesn't meet requirements."); 

Check the title again. I corrected it. I meant it doesn't work using negation whereas it should?

Ah okay I see now, you are basically trying to reverse it. I'm not entirely sure whether or not that would work under normal circumstances, but for these questions they are pretty strict in how they want you to answer the question - so that is probably the issue here.

Actually, I am fairly sure that would not deliver the desired results under normal circumstances either. It wouldn't produce an error, but it would give you different results than what they are looking for.