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

Gabe Olesen
Gabe Olesen
1,606 Points

Was my code OK?

Hi All,

I just finished this challenge and I wanted to ask the community if my code was OK for this task and if I should of done it a different/better way?

private String normalizeDiscountCode(String code) {
    this.discountCode = code.toUpperCase(); 
    for (char letter : discountCode.toCharArray()) {
      if (Character.isDigit(letter) || letter == '@') {
        throw new IllegalArgumentException("Invalid discount code.");
      }
    }
    return discountCode;
  }
Ian Taylor
Ian Taylor
Courses Plus Student 1,420 Points

Hi Gabe your code worked for me. Thank you! Not sure if there is a better way. I tried this but it kept throwing the error for the $ sign. char[] chars = discountCode.toCharArray(); for (char c : chars){ if ( !(Character.isDigit('$')) && !(Character.isLetter(c))){
throw new IllegalArgumentException("Invalid discount code"); }