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

How does it now what a Character is?

WHy in the if statement is it called a Character and not a char, when checking if it is a letter, and i don't really understand the enhanced for loop either, how does it know what the letter is?

Order.java
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 void applyDiscountCode(String discountCode) {
    this.discountCode = normalizeDiscountCode(discountCode);
  }
 private String normalizeDiscountCode(String discountCode) {
    return discountCode.toUpperCase();
    for (char letter : discountCode.toCharArray()){
      if(Character  
}
}

2 Answers

Hi Kristoffer,

From my understanding (and there's better brains in this forum than mine!) a char is a primitive data type so it is just a representation of the bytes that form a character (deliberate lower case c). The Character class was created to provide class-like qualities to a char. It supplies methods like toUpperCase, toLowerCase and for this challenge isLetter, for example.

The Java Documents list the numerous static class methods that can be called using Character.method() like we're using the isLetter method here - at least that's my reading of them!

So, char represents the data of an individual character. The Character class encapsulates behaviour to enable that char data to behave as an object. To quote the docs, The Character class wraps a value of the primitive type char in an object. An object of type Character contains a single field whose type is char.

The enhanced loop works only on iterables. So you pass it something that is capable of being iterated over, such as an array. Hence, we convert the string into an array of char data types. The loop knows to take each element in turn and store it in your char letter. Does that help?

Steve.

It is celled Character because char is reserved word.