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 trialAdam Shockley
4,548 PointsException error
Error is pointing to my exception on line 32
public class Order {
private String itemName;
private int priceInCents;
private String discountCode;
public Order(String itemName, int priceInCents) {
this.itemName = itemName;
this.priceInCents = priceInCents;
}
private String normalizeDiscountCode(String discountCode) {
return discountCode.toUpperCase();
}
public String getItemName() {
return itemName;
}
public int getPriceInCents() {
return priceInCents;
}
public String getDiscountCode() {
return discountCode;
}
public void applyDiscountCode(String discountCode) {
for (int i = 0; i < discountCode.length(); i++) {
char check = discountCode.charAt(i);
if (! Character.isLetter(check) || check != '$') {
throw new IllegalArgumentException("Invalid discount code.");
}
}
this.discountCode = normalizeDiscountCode(discountCode);
}
}
3 Answers
Steve Hunter
57,712 PointsHi Adam,
The question says In the normalizeDiscountCode verify .... Your code is in a different method. I'd also look at the enhanced for
loop to make that code easier.
Have a go at that, and let me know how you get on.
Steve.
Adam Shockley
4,548 PointsI moved it to the correct method and switched to an enhanced. Still receiving the error. Any ideas?
public class Order {
private String itemName;
private int priceInCents;
private String discountCode;
public Order(String itemName, int priceInCents) {
this.itemName = itemName;
this.priceInCents = priceInCents;
}
private String normalizeDiscountCode (String discountCode) {
for (char check : discountCode.toCharArray()) {
if (! Character.isLetter(check) || check != '$') {
throw new IllegalArgumentException("Invalid discount code");
}
}
return discountCode.toUpperCase();
}
public String getItemName() {
return itemName;
}
public int getPriceInCents() {
return priceInCents;
}
public String getDiscountCode() {
return discountCode;
}
public void applyDiscountCode(String discountCode) {
this.discountCode = normalizeDiscountCode(discountCode);
}
}
Adam Shockley
4,548 PointsGot it! The 'or' should have been an 'and'. The error was not at all helpful though.
Cheers.
Steve Hunter
57,712 PointsGood work!