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

Dorukhan Arat
Dorukhan Arat
31,325 Points

I'm stuck at Applying the discount code challenge task of 2 in java. Could anyone solve it?

Can anyone show me the solution of this exercise. I'm spend a lot of time on it and I can't find the answer and I'm curious about it.

Can you post your code - we can then help with correcting that.

Essentially, the method receives the discount code which is a string. You need to iterate over that string using a for loop (or use a regex) and test each character of the string - you'll want to convert the string into an array of characters to achieve this. If each character is a not letter and not a dollar sign; throw an exception. On completing the loop, assuming no exception has been thrown, return the uppercase version of the received string.

Steve.

I wrote this

if (discountCode.contains("1") || discountCode.contains("@")||discountCode.contains("#"){
throw new Illegal ....(blablabla)
}

And it worked!(maybe cheated but it worked) Allthough idea with loop and Array is good, but not with my skills!

Hi Bogdan,

I couldn't get your code to pass the challenge. The exception should be thrown if any number, or any non-letter excluding $, is in the passed-in discount code. You have only tested for one number; 1. If the user's discount code contained a 2, your exception wouldn't be thrown. Same if the discount code contain, for example, a % or a !.

The contains method is useful for many things but not where you want to test for lots of content as you have to write too many conditions.This is where regular expressions are incredibly useful as you can test for lots of content at once. There's a course for those which is very useful.

But the for loop is what this challenge is aimed at as that's relevant to the course content at this point.

Steve.

4 Answers

Hi there,

There's a couple of points here. First, don't set this.discountCode in this method. That's done in the applyDiscountCode method. So, remove that line.

Next, the return should send back the upper case version - so add .toUpperCase() on your return line.

Lastly, think about your logic. You've combined the two conditions using ||, an OR condition. You want both conditions (negated, correctly) to be true to throw the exception. You want to compare with &&.

Steve.

Dorukhan Arat
Dorukhan Arat
31,325 Points

I'm so grateful for your help steve thanks

No problem! :+1: :+1:

Hi Steve! Based on what we have learned i can say that arrays will be soon,didn't learn it yet. And in videos we didn't have anything like it(cheking for numbers and simbles) or i missed it. I used what i have learned so far and it helped me to check. It is not ideal, where your way would be better. I just do not know how to combine loops and arrays yet.

Hi Bogdan,

There's a previous challenge called "counting scrabble tiles" that uses a for loop. The segment of the course prior to that details the use of a for loop to iterate over a string that has been converted to an array. Have a look at around the 5:00 part of this course that precedes the current challenge.

Steve.

Dorukhan Arat
Dorukhan Arat
31,325 Points
private String normalizeDiscountCode (String discountCode) {
    this.discountCode = discountCode.toUpperCase();
    for(char c : discountCode.toCharArray()) {
        if (!(Character.isLetter(c)) || !(c == '$')) {
          throw new IllegalArgumentException ("Invalid discount code.");
        }
      }
    return discountCode;
  }

its look like this but it doesn't return the string uppercase

Steve! Thank you ! Helped me too with loops and arrays!

No problem! :+1: