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 Validating and Normalizing User Input

Bug with the exercise, maybe? (I solved it but I don't know how). Please explain!

/* Update: I deleted the try catch block from applyDiscountCode method and it worked. However, aren't I suppose to catch the exception there? */

This is what I'm getting when checking my work: Bummer! Hmmm...I ran order.applyDiscountCode("h1!") and I expected it to throw an IllegalArgumentException, but it didn't.

Here is my code:

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

public void applyDiscountCode(String discountCode) {
try {
this.discountCode = normalizeDiscountCode(discountCode);
} catch (IllegalArgumentException iae) {
  System.out.println(iae.getMessage());  
}
}

However, the preview even shows the message from the exception: Invalid discount code.

So it indeed does throw and catch the exception. Why does the challenge say that it doesn't?

Thank you!

Esteban Salinas
Esteban Salinas
1,873 Points

Arthur you don't need to change anything in the applyDiscountCode method and make sure "discountCode = discountCode.toUpperCase();" is in before "for" and change "return discountCode.toUpperCase();" to just "return discountCount;". Hope this helps. :)

6 Answers

Hi

/* Update: I deleted the try catch block from applyDiscountCode method and it worked. However, aren't I suppose to catch the exception there? */

The catch block is handling the exception for you.

I am not very familiar with challenge .... it it looks like Treehouse does not want you to handle exception ... basically, they want it thrown back to main

hope this helps. If this answers you question, pleas mark it ans answered.

Thanks for doing all the hard work for me guys. The help was much needed. This is the hard part about treeHouse I am finding. I never know if the solution is simple and I am overthinking it or they purposefully are expecting me to come up with some elaborate solution that requires hours of stackoverflow and forums posts. Oh well. #movingforward.

Hi

Do you || or && ???

Please check

naga bonar
naga bonar
3,338 Points

Hi Mark,

I use || "or" since it says the string contains of letters or $ (dollar sign).

Thank you.

Use && instead of || as displayed below:

if(!Character.isLetter(letter) && letter != '$')

I had the same code as yours @nagabonar and couldn't find the reason for my code failure. Hope the above change resolves your task.

Can Uludag
Can Uludag
Courses Plus Student 8,769 Points

Suprisingly, changing if(!Character.isLetter(letter) || letter != '$') to if(!Character.isLetter(letter) && letter != '$') solved the issue. But I think it is against the rules of the challenge. Letter should not be numeric character. It can only be a letter or dollar sign. So || (or) is much more understandable than && (and) logic.

naga bonar
naga bonar
3,338 Points

I got a similar problem too in this exercise. The exercise goal is to check the argument that passed into normalizeDicountCode is need to be verified rather they are letters or '$' sign. But it keeps telling me that Task 1 is no longer passing.

private String normalizeDiscountCode(String code){

    for(char letter : code.toCharArray()){
      if(!Character.isLetter(letter) || letter != '$'){
        throw new IllegalArgumentException("Invalid discount code.");
      }
    }
    discountCode = code.toUpperCase();
    return discountCode;
  }

Please, i need help to get pass this exercise. it's killing me.

You should use && instead of ||. If you use && all the letters are going to throw an iae because they are not a $.

Esteban Salinas
Esteban Salinas
1,873 Points

+Naga Bonar, Wasn't it "normalizeDiscountCode(String discountCode)" in first part of the challenge?

Odd, shouldn't it be || instead of && since the questions says it should either be a letter OR a $?