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 trialMihai Andrei
612 PointsThrowing 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
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
10,810 PointsYour 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
612 PointsThanks a lot, Allan!
Jess Sanders
12,086 Pointsprivate 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"
Allan Clark
10,810 PointsAllan Clark
10,810 Pointsp.s. API documentation: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
Hope this helps!