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

Task one no longer passing after submitting task 2

I have written the below method for normalizeDiscountCode, i can't find any errors with it but when i hit the button to check it I am getting an error with Task One even though i already completed that one. I have had this happen for other challenges but it was a syntax error but i cannot find one here.

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

[MOD: edited post to relevant code - srh]

2 Answers

Agree - due to the required double negation, an && is needed not an || - work through the logic as, predictably, it does make sense!

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

Steve.

That worked thank you! That does actually make sense now that i think about it