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

Applying a Discount Code 2-2.

Im pretty stuck here.. I feel like im close, but when i preview the code it says: ./Order.java:7: error: incompatible types: String cannot be converted to char for (char element : discountCode.split("")).

I really find the java debugger to be useless, rarely do i understand what it is actually saying. Help would be much appreciated!

Order.java
public class Order {
  private String itemName;
  private int priceInCents;
  private String discountCode;

  private String normalizeDiscountCode(String discountCode) { // THIS IS WHERE THE CHALLENGE IS!
    for (char element : discountCode.split("")) {
      if (! Character.isLetter(element)) {
        if (element != '$') {
          throw new IllegalArgumentException("Invalid discount code.");
        }
      }
    }
    return discountCode.toUpperCase();
  }

  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);
  }
}

2 Answers

Juraj Sallai
Juraj Sallai
7,188 Points

Look at line 7. You cannot assing char element to String. Split() method does not return char, but String. So instead of split(), think about using toCharArray() method for your discountCode.

Got it, ty for taking the time Juraj!

Juraj Sallai
Juraj Sallai
7,188 Points

Glad to help, good luck!