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

dan le
PLUS
dan le
Courses Plus Student 2,196 Points

Validation

Hi, I could not find why it kept telling me this: ./TeacherAssistant.java:20: error: missing return statement } ^ 1 error

Here is my code: public class TeacherAssistant { public String mFieldName; 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 //char letter = '\0'; fieldName = fieldName.toLowerCase(); for ( int i = 0; i < fieldName.length() ; i++) { char c = fieldName.charAt(i);

     if( ! Character.isLetter(c)) 
      {
        throw new IllegalArgumentException("Must be letter");
      }
      return fieldName;

 } 

} }

TeacherAssistant.java
public class TeacherAssistant {
  public String mFieldName;
  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
    //char letter = '\0';
    fieldName = fieldName.toLowerCase();
    for ( int i = 0; i < fieldName.length() ; i++) {
          char  c = fieldName.charAt(i);

         if( ! Character.isLetter(c)) 
          {
            throw new IllegalArgumentException("Must be letter");
          }
          return fieldName;

     } 
   } 
}

1 Answer

Because your return statement is in a loop, the compiler wants you to have one after the loop as well. This is because Java doesn't know if the loop will execute more than zero times. If it were to execute zero times for some reason, as you have written it, the method would not know what String to throw back to the calling routine. That is why you are getting the error.

That being said, in this case, I don't think you meant to have your return statement inside your loop.

Also: You are converting the String fieldName to all lowercase before your loop. If it is all lowercase, you will not be able to check for the camel-casing part of the question. You can check if a character is uppercase by using the Character.isUpperCase(char) method.

Character.isUpperCase(fieldName.charAt(1)); 
//Returns true if the character passed in was uppercase