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

Jeff Janes
Jeff Janes
8,033 Points

Validation Challenge

Here's my current code...I'm probably making it more complicated than it needs to be. How else would I require it to meet both without the &&s?

public class TeacherAssistant {

  public static String validatedFieldName(String fieldName) {

    if (fieldName.charAt(0)) = 'm') && (! Character.isUpperCase(fieldName.charAt(1))) {
      throw new IllegalArgumentException("Invalid field name");
    } 
    return fieldName;
  }

}
Jeff Janes
Jeff Janes
8,033 Points

Got it figured out! This code worked for me.

public class TeacherAssistant {

  public static String validatedFieldName(String fieldName) {

    if (fieldName.indexOf(0) != 'm' && (!Character.isUpperCase(fieldName.charAt(1)))) {
      throw new IllegalArgumentException("Invalid field name");
    } 
    return fieldName;
  }

}
Sangjae Choi
Sangjae Choi
3,261 Points

I don't understand how we would give an illegal argument only when the first character is not 'm' AND the second character is not uppercase.

Like... shouldn't we throw an illegal argument exception even if the first character is not 'm' but the second character is uppercase?

Likewise, shouldn't we throw an illegal argument exception even if the first character is 'm' but the second character is not uppercase?

Sangjae Choi
Sangjae Choi
3,261 Points

I wrote:

if (fieldName.charAt(0) != 'm') {
      throw new IllegalArgumentException("The first character must be 'm'");
    } else if (!Character.isUpperCase(fieldName.charAt(1))) {
      throw new IllegalArgumentException("The second character must be an uppercase letter");
    }
    return fieldName;
Anton Ditmarov
Anton Ditmarov
5,085 Points
public class TeacherAssistant {

      public static String validatedFieldName(String fieldName) {

        if (fieldName.charAt(0) == 'm' && 
            !Character.isUpperCase(fieldName.charAt(1))) {
          throw new IllegalArgumentException("Invalid field name");
        } 
        return fieldName;
      }

    }

Honestly, no one told he poor guy he was using "=" instead of "=="? = sets variables == checks for object equality (use .equals(Object object) method for objects)

*I didn't read the problem, just fixed the first code to a working version and less parenthesis. But seriously, the most important error was the = which not a single person caught and then another person made the same mistake.

5 Answers

A couple things. You have 2 extra ending parenthesis on the fieldName.charAt(0) = 'm" parameter. When you use "and" statement in the if loop then if BOTH conditions are true, then you throw the exception as you've currently written it.

  1. You want an "or" statement, so that if either condition is true (meaning either mistake in naming conventions has been made) the if loop throws the exception.
  2. The exercise suggests using your nor statements. What you want to say is that if the character at the string index of zero is NOT m, then throw the exception. Currently, that's not what is says.
  3. Take it easy on the parenthesis. Think of the methods inside the conditional as having them already.
  4. I'm pretty sure the validateFieldName method doesn't have to be static, or viewable to other classes without reference to it's native class.

Try following that. The answer above is incorrect.

Happy coding! Nicolas

Anton Ditmarov
Anton Ditmarov
5,085 Points
public class TeacherAssistant {

      public static String validatedFieldName(String fieldName) {

        if (fieldName.charAt(0) == 'm' && 
            !Character.isUpperCase(fieldName.charAt(1))) {
          throw new IllegalArgumentException("Invalid field name");
        } 
        return fieldName;
      }

    }

Honestly, no one told he poor guy he was using "=" instead of "=="? = sets variables == checks for object equality (use .equals(Object object) method for objects)

*I didn't read the problem, just fixed the first code to a working version and less parenthesis. But seriously, the most important error was the = which not a single person caught and then another person made the same mistake.

Anton Ditmarov
Anton Ditmarov
5,085 Points
public class TeacherAssistant {

  public static String validatedFieldName(String fieldName) {

    if (fieldName.charAt(0) != 'm') {
      throw new IllegalArgumentException("The first letter must be m");
    }
    if(Character.toUpperCase(fieldName.charAt(1)) != fieldName.charAt(1)
       || !Character.isLetter(fieldName.charAt(1))) {
      throw new IllegalArgumentException("Second letter must be Uppercased and a letter");
    }
    return fieldName;
  }

}

My code...didn't know .isUpperCase method existed. Could be shorter, but I think it's good (for the challenge) =D

public class TeacherAssistant {

  public String validateFieldName(String fieldName) {

    if (fieldName.charAt(0) = 'm' 
    || (!Character.isUpperCase(fieldName.charAt(1))
    || (!Character.isLetter(fieldName.charAt(0))))) {
      throw new IllegalArgumentException("Invalid field name.");
    } 
    return fieldName;
  }

}

should work.

Jeff Janes
Jeff Janes
8,033 Points

No luck unfortunately. Getting these errors...

./TeacherAssistant.java:7: error: ')' expected
    || (!Character.isLetter(fieldName.charAt(0))))) {
                                                   ^
JavaTester.java:42: error: cannot find symbol
      TeacherAssistant.validatedFieldName(failure);
                      ^
  symbol:   method validatedFieldName(String)
  location: class TeacherAssistant
JavaTester.java:55: error: cannot find symbol
      TeacherAssistant.validatedFieldName(success);
                      ^
  symbol:   method validatedFieldName(String)
  location: class TeacherAssistant
./TeacherAssistant.java:5: error: unexpected type
    if (fieldName.charAt(0) = 'm' 
                        ^
  required: variable
  found:    value
./TeacherAssistant.java:7: error: bad operand types for binary operator '||'
    || (!Character.isLetter(fieldName.charAt(0))))) {
    ^
  first type:  char
  second type: boolean
5 errors