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 trialJeff Janes
8,033 PointsValidation Challenge
Here's my current code...I'm probably making it more complicated than it needs to be. How else would I require it to meet both without the &&s?
public class TeacherAssistant {
public static String validatedFieldName(String fieldName) {
if (fieldName.charAt(0)) = 'm') && (! Character.isUpperCase(fieldName.charAt(1))) {
throw new IllegalArgumentException("Invalid field name");
}
return fieldName;
}
}
Sangjae Choi
3,261 PointsI don't understand how we would give an illegal argument only when the first character is not 'm' AND the second character is not uppercase.
Like... shouldn't we throw an illegal argument exception even if the first character is not 'm' but the second character is uppercase?
Likewise, shouldn't we throw an illegal argument exception even if the first character is 'm' but the second character is not uppercase?
Sangjae Choi
3,261 PointsI wrote:
if (fieldName.charAt(0) != 'm') {
throw new IllegalArgumentException("The first character must be 'm'");
} else if (!Character.isUpperCase(fieldName.charAt(1))) {
throw new IllegalArgumentException("The second character must be an uppercase letter");
}
return fieldName;
Anton Ditmarov
5,085 Pointspublic class TeacherAssistant {
public static String validatedFieldName(String fieldName) {
if (fieldName.charAt(0) == 'm' &&
!Character.isUpperCase(fieldName.charAt(1))) {
throw new IllegalArgumentException("Invalid field name");
}
return fieldName;
}
}
Honestly, no one told he poor guy he was using "=" instead of "=="? = sets variables == checks for object equality (use .equals(Object object) method for objects)
*I didn't read the problem, just fixed the first code to a working version and less parenthesis. But seriously, the most important error was the = which not a single person caught and then another person made the same mistake.
5 Answers
Nicolas Hampton
44,638 PointsA couple things. You have 2 extra ending parenthesis on the fieldName.charAt(0) = 'm" parameter. When you use "and" statement in the if loop then if BOTH conditions are true, then you throw the exception as you've currently written it.
- You want an "or" statement, so that if either condition is true (meaning either mistake in naming conventions has been made) the if loop throws the exception.
- The exercise suggests using your nor statements. What you want to say is that if the character at the string index of zero is NOT m, then throw the exception. Currently, that's not what is says.
- Take it easy on the parenthesis. Think of the methods inside the conditional as having them already.
- I'm pretty sure the validateFieldName method doesn't have to be static, or viewable to other classes without reference to it's native class.
Try following that. The answer above is incorrect.
Happy coding! Nicolas
Anton Ditmarov
5,085 Pointspublic class TeacherAssistant {
public static String validatedFieldName(String fieldName) {
if (fieldName.charAt(0) == 'm' &&
!Character.isUpperCase(fieldName.charAt(1))) {
throw new IllegalArgumentException("Invalid field name");
}
return fieldName;
}
}
Honestly, no one told he poor guy he was using "=" instead of "=="? = sets variables == checks for object equality (use .equals(Object object) method for objects)
*I didn't read the problem, just fixed the first code to a working version and less parenthesis. But seriously, the most important error was the = which not a single person caught and then another person made the same mistake.
Anton Ditmarov
5,085 Pointspublic class TeacherAssistant {
public static String validatedFieldName(String fieldName) {
if (fieldName.charAt(0) != 'm') {
throw new IllegalArgumentException("The first letter must be m");
}
if(Character.toUpperCase(fieldName.charAt(1)) != fieldName.charAt(1)
|| !Character.isLetter(fieldName.charAt(1))) {
throw new IllegalArgumentException("Second letter must be Uppercased and a letter");
}
return fieldName;
}
}
My code...didn't know .isUpperCase method existed. Could be shorter, but I think it's good (for the challenge) =D
Anton Ditmarov
5,085 PointsSorry Triple Post =(
ISAIAH S
1,409 Pointspublic class TeacherAssistant {
public String validateFieldName(String fieldName) {
if (fieldName.charAt(0) = 'm'
|| (!Character.isUpperCase(fieldName.charAt(1))
|| (!Character.isLetter(fieldName.charAt(0))))) {
throw new IllegalArgumentException("Invalid field name.");
}
return fieldName;
}
}
should work.
Jeff Janes
8,033 PointsNo luck unfortunately. Getting these errors...
./TeacherAssistant.java:7: error: ')' expected
|| (!Character.isLetter(fieldName.charAt(0))))) {
^
JavaTester.java:42: error: cannot find symbol
TeacherAssistant.validatedFieldName(failure);
^
symbol: method validatedFieldName(String)
location: class TeacherAssistant
JavaTester.java:55: error: cannot find symbol
TeacherAssistant.validatedFieldName(success);
^
symbol: method validatedFieldName(String)
location: class TeacherAssistant
./TeacherAssistant.java:5: error: unexpected type
if (fieldName.charAt(0) = 'm'
^
required: variable
found: value
./TeacherAssistant.java:7: error: bad operand types for binary operator '||'
|| (!Character.isLetter(fieldName.charAt(0))))) {
^
first type: char
second type: boolean
5 errors
Jeff Janes
8,033 PointsJeff Janes
8,033 PointsGot it figured out! This code worked for me.