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
ignaciodelatorre
16,335 PointsHow to solve this Task ? I´m driving crazy :#
Now let's use your validation skills. Only letters and the $ symbols are allowed in the discount code. Check Example.java for use cases In the normalizeDiscountCode verify that only letters or the $ character are used. If any other character is used, throw a IllegalArgumentException with the message Invalid discount code.
private String normalizeDiscountCode(String code) { char arr[] = new char[25]; arr = code.toCharArray(); int i; for(i= 0; arr[i] != '\0'; i++) { if(arr[i] != '$' || (Character.isLetter(arr[i]) == false) ) { throw new IllegalArgumentException("Error"); } } this.discountCode = code.toUpperCase(); return discountCode; } }
I´ve done this, but then it says that Task number one is not useful anymore. What am I doing wrong ?
2 Answers
Mihai Craciun
13,520 PointsThink about the task. You have to throw the exception if the code does not contain letter AND the '$' is an exception. In this case all you have to do is replace || (OR symbol) with && (AND symbol) in your if statement and then try to use a for each statement so you will not need any bounds. Below you will find my solving code based on your code.
private String normalizeDiscountCode(String code) {
for(char letter: code.toCharArray()) {
if(letter != '$' && (Character.isLetter(letter) == false) ) {
throw new IllegalArgumentException("Error");
}
}
this.discountCode = code.toUpperCase();
return discountCode;
}
ignaciodelatorre
16,335 PointsYou are the best Mihai :) Thanks for that information and for taking the time to code it for me! Cheers from Argentina brother !