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

Benjamin Gooch
Benjamin Gooch
20,367 Points

I'm trying to store a character from a string as a variable but it's not working.

Hello everyone,

I'm trying to complete the code challange following this video and I'm having an issue with it. I'm not getting any errors from my code (at least as far as the "preview" button is concerned) but it does throw both of my exceptions. Also, when I click to progress, it says it expected "firstName" but didn't get it. I'm not too sure what I'm doing wrong here.

I looked at someone else's code and it appears that maybe the issue is that I'm trying to store the 2nd character from fieldName in the variable secondLetter and run the check on the variable, but it's not processing correctly or something. Can someone please explain what I'm doing wrong here? If my assumption is correct, what is the reason that I can't do it this way?

Thanks!

public class TeacherAssistant {
  public boolean startsWithM = false;
  public static char firstLetter;
  public static char secondLetter;
  public static char correctFirstLetter = 'm';

  public static String validatedFieldName(String fieldName) {
    firstLetter = fieldName.charAt(0);
    secondLetter = fieldName.charAt(1);
    // 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

    try {
      if (firstLetter != correctFirstLetter) {
        throw new IllegalArgumentException("First letter must be a lower case 'm'.\n");
      }
    } 
    catch (IllegalArgumentException flnm) {
      System.out.printf("%s.\n", flnm.getMessage());
    }

    try {
      if (!Character.isUpperCase(secondLetter)) {
        throw new IllegalArgumentException("Second letter must be upper case.\n");
      }
    } 
    catch (IllegalArgumentException slnuc) {
      System.out.printf("%s.\n", slnuc.getMessage());
    }

    return fieldName;
  }


}
Benjamin Gooch
Benjamin Gooch
20,367 Points

sigh Nevermind, Looks like I overdid the challenge. I took out the try blocks and the catch blocks so it was reduced to just if statements and it passed the check, even with the variable for secondLetter being used in the check.

Now I'm curious why it was failing. If the code in the try block fails, shouldn't it have failed the validation all together? According to the thing that says "Bummer!" the reason I wasn't passing is because "firstName" was passing validation and it was supposed to fail.

Any ideas?