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

Johnathan Mullen
Johnathan Mullen
1,868 Points

Apply Discount Code

I passed the first task and now I think I am passing the second one but it is telling me the first task no longer is passing. I use the same code and it passes the first one again then on the second task it says not passing first one again ...Please help

Order.java
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 void applyDiscountCode(String discountCode) {
    this.discountCode = normalizeDiscountCode(discountCode);
  }
  private String normalizeDiscountCode(String discountCode){
    for (char letter : discountCode.toCharArray()){
   if(! Character.isLetter(letter) || letter != '$' || letter != '@'){
   throw new IllegalArgumentException("Invalid discount code");
   }
    }
    return discountCode.toUpperCase();
  }  
}

2 Answers

Could you send the stack trace of the error you are getting possibly or what is showing up in the console.

Anders Björkland
Anders Björkland
7,481 Points

Hi Johnathan!

This is a classic. You are not the first to take this stumble. You don't have to go back to task 1 and fix it. You cleared task 1, and it is task 2 that is your challenge right now. You are actually having a problem in your normalizeDiscountCode-method. The problem is that you will always be throwing an exception when you are checking any character, since a letter can't be a '$'-character and vice versa. (You can ignore checking '@'-character, I believe you are not looking for that one in the challenge.)

So what is throwing you off is this line:
if(! Character.isLetter(letter) || letter != '$')
You need to rephrase it somehow, so that you will throw an exception if character is not a letter and not a '$'-character.
Hope this helps!