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 trialGabriel E
8,916 PointsHow do I get the ThrowException to work?
Hi there,
I am having a might of trouble with this task. I am supposed to get some specific requirements to be passed, and if they don't pass as true, then I throw the Exception. I already received an answer earlier, and I altered my code to what I received in the response, but I'm getting 1 error: the =='m' is apparently not valid. If you could include some code, that would be great! Thanks for the help!
public class TeacherAssistant {
public static String validatedFieldName(String fieldName) {
fieldName.charAt(0) == 'm';
Character.isUpperCase(fieldName.charAt(1));
if(fieldName.charAt(0) == 'm' && Character.isUpperCase(fieldName.charAt(1))) {
/// both things are true - do whatever success looks like
} else {
throw new IllegalArgumentException("This does not meet the requirements!");
//throw that exception
}
// 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
Steve Hunter
57,712 PointsThe answer you had was from me - I walked you through the code. If you're having trouble with that, it's probably best to flag that in the original post.
If you delete the first two lines you've added there so that the final code looks like:
public class TeacherAssistant {
public static String validatedFieldName(String fieldName) {
if(fieldName.charAt(0) == 'm' && Character.isUpperCase(fieldName.charAt(1))) {
// both things are true - do whatever success looks like
} else {
throw new IllegalArgumentException("This does not meet the requirements!");
}
return fieldName;
}
}
That should be right. The other two lines were just me explaining what to do in the forum post.
I'll check, but I'm pretty sure the above will pass, else I'll be back to amend it!!
Steve.
Gabriel E
8,916 PointsThanks Steve, I'd like to say it worked, but when I pasted that exact code in, it says that the problem is something with =='m'. The little arrow carrot is pointing up at the 'm' in the fieldName.charAt method. I'm not sure what to do about this.
Steve Hunter
57,712 PointsVery odd ... did you remove the two lines of code that were not doing much - they looked like:
fieldName.charAt(0) == 'm';
Character.isUpperCase(fieldName.charAt(1));
The first line of your added code should start with the if
.
If you jumped on my previous reply really quickly, you probably caught me mid-edit! So that may mean you pasted the wrong code in.
The code above does work. :-)
Steve.
Gabriel E
8,916 PointsNever mind, I got it. Thanks for your help!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsConfirmed to work. :-)