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

Java Java Objects Delivering the MVP Applying a Discount Code

In the normalizeDiscountCode verify that only letters or the $ character are used. What am I doing wrong?

public class Order { private String itemName; private int priceInCents; private String discountCode;

public Order(String itemName, int priceInCents) { this.itemName = itemName; this.priceInCents = priceInCents; }

public String getItemName() { return itemName; }

public int getPriceInCents() { return priceInCents; }

public String getDiscountCode() { return discountCode; }

public void applyDiscountCode(String discountCode) { this.discountCode = normalizeDiscountCode(discountCode); }

private String normalizeDiscountCode(String discountCode) { for(char letter : discountCode.toCharArray()) { if(!Character.isLetter(letter) || letter != '$'){ throw new IllegalArgumentException("Invalid discount code"); }else{ return discountCode.toUpperCase(); } } } }

3 Answers

Thank you it was not just the operator || since I had already tried it in both ways but it did not work, in the same way the else considered it wrong and that is the solution

public class Order {

private String itemName; 

private int priceInCents; 

private String discountCode;

public Order(String itemName, int priceInCents) {

this.itemName = itemName; 

this.priceInCents = priceInCents; 

}

public String getItemName() {

return itemName; 

}

public int getPriceInCents() {

return priceInCents; 

}

public String getDiscountCode() {

return discountCode; 

}

public void applyDiscountCode(String discountCode) {

this.discountCode = normalizeDiscountCode(discountCode); 

}

private String normalizeDiscountCode(String discountCode) {

for(char letter : discountCode.toCharArray()) { 

  if(!Character.isLetter(letter) && letter != '$'){ 

    throw new IllegalArgumentException("Invalid discount code"); 

  }     

} 

return discountCode.toUpperCase();

}

}

alastair cooper
alastair cooper
30,617 Points

can't help you without seeing your code, but the error message would suggest that there is a method that should return a value that doesn't return anything at the moment

I'm sorry I do not know how to share it

alastair cooper
alastair cooper
30,617 Points

!Character.isLetter(letter) || letter != '$'

Your problem is the logic in the if statement. Not this OR Not that allows almost anything.

Try changing the || to &&

if it is not a letter AND it is not $ ... ie !Character.isLetter(letter) && letter != '$'