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 trialM Costa
1,621 PointsQuestion on the below task: "In the normalizeDiscountCode verify that only letters or the $ character are used."
I've already passed this task using '&&' but can someone explain why I can't use '||' as listed below?
I get the error 'Bummer: java.lang.IllegalArgumentException': ABC must only contain letters or '$' signs (Look around Order.java line 19)
Also I couldn't tell if this was my throw exception or a java error.
Much appreciated!
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 discount){
discountCode = discountCode.toUpperCase();
discount = discountCode;
// char[] discountChar = discount.toCharArray();
for(char discountChar : discount.toCharArray()){
if (! Character.isLetter(discountChar) || discountChar !='$'){
throw new IllegalArgumentException(discount + " must only contain letters or '$' signs");
}
} return discount;
}
public String getItemName() {
return itemName;
}
public int getPriceInCents() {
return priceInCents;
}
public String getDiscountCode() {
return discountCode;
}
public void applyDiscountCode(String discountCode) {
this.discountCode = discountCode;
discountCode = normalizeDiscountCode(discountCode);
}
}
public class Example {
public static void main(String[] args) {
// This is here just for example use cases.
Order order = new Order(
"Yoda PEZ Dispenser",
600);
// These are valid. They are letters and the $ character only
order.applyDiscountCode("abc");
order.getDiscountCode(); // ABC
order.applyDiscountCode("$ale");
order.getDiscountCode(); // $ALE
try {
// This will throw an exception because it contains numbers
order.applyDiscountCode("ABC123");
} catch (IllegalArgumentException iae) {
System.out.println(iae.getMessage()); // Prints "Invalid discount code"
}
try {
// This will throw as well, because it contains a symbol.
order.applyDiscountCode("w@w");
}catch (IllegalArgumentException iae) {
System.out.println(iae.getMessage()); // Prints "Invalid discount code"
}
}
}
1 Answer
Daniel Turato
Java Web Development Techdegree Graduate 30,124 PointsThe challenge states that the discount has to be a letter or contain the $ character. Therefore, to check for a failure on this occurrence it can NOT be a letter or contain the $ symbol. What you did was that it can't be a letter or it can't be equal to the $ character meaning even if a non letter was passed in the discount code, it would still pass through the if statement as its not equal to '$' resulting in true for the OR logic. Here is the correct code by using OR:
if (!(Character.isLetter(discountChar) || discountChar == '$')) {
throw new IllegalArgumentException(discount + " must only contain letters or '$' signs");
}