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

Urooba Urooba
Urooba Urooba
3,565 Points

unable to understand

i think the code is ok but it keeps on returning the error that the letters are not being uppercase

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 = discountCode;

    }catch(IllegalArgumentException iae)
  private String normalizeDiscountCode(String result)
    for (char letter: result.toCharArray())
      throw new IllegalArgumentException("Invalid discount code");
    return result;
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"


1 Answer

28,558 Points

That error message is indeed a bit strange, I can't quite see why that is the error message produced, but I can see the issues with your code.

There are two issues:

  1. You use a semicolon ; on the line where you declare your if statement, that is incorrect and will cause the if statement to be terminated prematurely. Meaning that the code that follows it will be executed always, regardless of what the conditions for the if statement is.

  2. The challenge asks you to throw an exception but does not ask you to catch said exception yourself, and in fact doing so will break the challenge. The code checker that verifies your code expects it to throw it an exception when given certain input, and uses that to validate that the code is working correctly. If you catch the exception yourself then it won't reach the code checker that runs the code.

So if you remove the wrongly placed semicolon ; and remove the try/catch block from your code then your code will work.