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

Mihai Andrei
Mihai Andrei
612 Points

Throwing an exception

I have to throw an exception if the string fieldName is not validating the 2 conditions:

public static String validatedFieldName(String fieldName) { if ( fieldName.indexOf(0)!=('m'))
{throw new IllegalArgumentException ("is not starting with an 'm'");} if (!('A'<= fieldName.indexOf(1)) && (fieldName.indexOf(1) <= 'Z')) {throw new IllegalArgumentException (" not uppercase");}

// 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
return fieldName;

//Thanks for answering

TeacherAssistant.java
public class TeacherAssistant {

 public static String validatedFieldName(String fieldName) {
    if ( fieldName.indexOf(0)!=('m'))  
  {throw new IllegalArgumentException ("Nu e ok");}
    if (!('A'<= fieldName.indexOf(1)) && (fieldName.indexOf(1) <= 'Z')) 
      {throw new IllegalArgumentException ("Nu e okey");}

    // 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
    return fieldName;
  }

}

3 Answers

Allan Clark
Allan Clark
10,810 Points

Your first if statement checks if the index of the int 0 is 'm'. The indexOf() method always returns an int so it will never be == to 'm' so it always throws the exception. You pass the indexOf() method a String, the method checks the String it was called on and returns the index of what was passed to it.

String str = "Hello World";

str.indexOf("H");
// returns 0

str.indexOf("z");
// returns -1

str.indexOf(492);
// returns -1
Mihai Andrei
Mihai Andrei
612 Points

Thanks a lot, Allan!

private char validateGuess(char letter) {
    if (!Character.isLetter(letter)) {
      throw new IllegalArgumentException("A letter is required."
    }
    letter = Character.toLowerCase(letter);
    if (mMisses.indexOf(letter) >= 0 || mHits.indexOf(letter) >= 0) { 
      throw new IllegalArgumentException(letter + " has already been guessed")           }
    return letter;
  }
  • The above code is the example to follow.

  • In pseudocode, we want to check for the following condition, and throw an error: " if the character at index 0 of fieldName does not equal 'm' OR the character at index 1 of fieldName is not upper case"