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

exercise to validate discountCode. my code for try-catch does not work. I raise the IllegalArgumentException.

Appreciate guidance on which method should contain code for try-catch block. I believe what I have in my 'try' is incorrect but don't know how to fix it. Thank you for your help.

Travis Alstrand
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travis Alstrand
Treehouse Project Reviewer

Hi there o2003 👋

Can you share with us the code that you've tried so we have an understanding on what may need some adjustments? Thanks! 😃

4 Answers

Travis Alstrand
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travis Alstrand
Treehouse Project Reviewer

So there were two things I saw but you were extremely close!

  1. We needed to be using an && in your conditional instead of an ||. This will check if the character is not a letter and it's not a $.

This should get it to pass, but I wanted to point out also that

  1. There was the addition of discountCode added to the error message and there was some other code added to the applyDiscountCode block below. Adding things not specifically asked for in these challenges can sometimes cause them not to pass. They're extremely picky 😅

Anyways, I hope this makes sense and helps.

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

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

Also, checkout the Markdown Cheatsheet to help make the code a bit more readable next time, you can wrap it within triple backticks 👍

my apology for the delay (I thought since I submitted the request from within the exercise, it automatically captured that broken code. I am sorry). Below is my code for second part of the exercise that require normalizeDiscountCode method. I did not change code for applyDiscountCode method from first part of the exercise. I get a compiler error about a semicolon.
Thank you for your guidance.

private String normalizeDiscountCode (String discountCode){ for (char c:discountCode.toCharArray()) { if (! Character.isLetter(c) || c != '$') { throw new IllegalArgumentException(discountCode + "Invalid discount code."); try { order.applyDiscountCode(discountCode); } catch (IllegalArgumentException iae) { System.out.println(iae.getMessage());}} return discountCode.toUpperCase(); }

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

follow up. I was able to fix the issue with try-catch block but this code does not return the discountCode when valid in all caps.
Entire error message given to me is "Bummer:Hmmm, I called order.applyDiscountCode("abc") and expected order.getDiscountCode = "ABC" but it was null." Thank you for your help.

the entire code of the part 2 exercise:

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; } private String normalizeDiscountCode (String discountCode){ for (char c:discountCode.toCharArray()) { if (! Character.isLetter(c) || c != '$') { throw new IllegalArgumentException(discountCode + "Invalid discount code."); } } return discountCode.toUpperCase(); } public void applyDiscountCode(String discountCode) { try {this.discountCode = normalizeDiscountCode (discountCode);} catch (IllegalArgumentException iae) { System.out.println(iae.getMessage());}
} }

Hi Travis, Thank you for your feedback and your suggestion worked. I want to apologize for how when I pasted my code, it lost its formatting (looks really embarrassing). This exercise and misleading guidance from the compiler really have dampened my enthusiasm (I did not get creative and added the catch-try block on my own...one of the 'Bummer...umm' messages, suggested it.). Thank you for Markdown Cheatsheet as well. Thank you.

Travis Alstrand
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travis Alstrand
Treehouse Project Reviewer

No worries about the formatting! I was just mentioning it for next time is all 👍

Yes, honestly I wish it was something easier to learn how to go in and edit these code challenges. They've given me grief in the past as well. Definitely post here if you start to get stuck though and one of us will help you out!