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!

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

What's wrong with my code?

compiler error: line 30

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();
    return discountCode.toUpperCase();
public class Example {

  public static void main(String[] args) {
    // This is here just for example use cases.

    Order order = new Order(
            "Yoda PEZ Dispenser",

    // These are valid.  They are letters and the $ character only
    order.getDiscountCode(); // ABC

    order.getDiscountCode(); // $ALE

    try {
      // This will throw an exception because it contains numbers
    } catch (IllegalArgumentException iae) {
      System.out.println(iae.getMessage());  // Prints "Invalid discount code"
    try {
      // This will throw as well, because it contains a symbol.
    }catch (IllegalArgumentException iae) {
      System.out.println(iae.getMessage());  // Prints "Invalid discount code"


2 Answers

Line 30 is the throw IllegalArgumentException. Your code is throwing the exception.

Of course, it shouldn't because it's just handling "abc" the first time through. If we look closely at the line before it, we can see what is going wrong:

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

If you think very carefully about the logic of the line, you might be able to see what is happening.

Look closely at this part of the line: (! Character.isLetter(letter) || letter !='$'). What will happen if you give it a '$'? Will it be a letter? No. Which you then negate with the !. That will be true, and so you will throw the exception.

Or if you send it a letter, the first part will be fine. However, letter!='$' will return true, and so it will again throw an exception.

You are doing the exact opposite of what you wanted to do.

There are several ways to solve it. You could negate the result of the whole thing. Or you can change the || to an and. Then you will saying, throw an error if the character is not a Letter AND letter not a dollar sign.

Hopefully that helps. If not, we can try to explain it further.

does that mean that the original negative (!) includes all arguments?

No the first negative should only apply to the first argument (the Character.isLetter(letter)).