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 trialZdenek Krcal
49,910 PointsPlease help with JAVA code challenge.
I think my code is correct, but I'm not able to finish this challenge.
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;
}
private String normalizeDiscountCode(String discountCode) {
for( char l : discountCode.toCharArray()) {
if(! Character.isLetter(l) || l != '$') {
throw new IllegalArgumentException("Invalid discount code");
}
}
return discountCode.toUpperCase();
}
public void applyDiscountCode(String discountCode) {
this.discountCode = normalizeDiscountCode(discountCode);
}
}
public class Example {
public static void main(String[] args) {
// This is here just for example use cases.
Order order = new Order(
"Yoda PEZ Dispenser",
600);
// These are valid. They are letters and the $ character only
order.applyDiscountCode("abc");
order.getDiscountCode(); // ABC
order.applyDiscountCode("$ale");
order.getDiscountCode(); // $ALE
try {
// This will throw an exception because it contains numbers
order.applyDiscountCode("ABC123");
} catch (IllegalArgumentException iae) {
System.out.println(iae.getMessage()); // Prints "Invalid discount code"
}
try {
// This will throw as well, because it contains a symbol.
order.applyDiscountCode("w@w");
}catch (IllegalArgumentException iae) {
System.out.println(iae.getMessage()); // Prints "Invalid discount code"
}
}
}
2 Answers
andren
28,558 PointsYour code is very close to correct, and the mistake you have made is one I have seem quite a few other students make. This mistake is likely common due to the way the requirement is phrased.
The mistake is that you are checking if the character is not a letter OR not the $ symbol, the way the OR operator works is that if either of the conditions are true if will return true and therefore run the if statement. Let's say the character is in fact a letter, the first condition will be false, but the second condition will return true so the if statement runs and an exception is thrown, even though a letter should not result in an exception. The same is true for the $ symbol, the first condition will be true and therefore the if statement will run even though the second condition is false.
One way around this is to use the && operator instead, that way the if statement will only run if the character is both not a letter and not the $ symbol, which is what the task specifies.
So to sum up, change the || operator in your if statement to && and your code will pass.
Raphaël Seguin
Full Stack JavaScript Techdegree Graduate 29,228 Pointstrue too :) ! It's either 'not this neither that' ( != && != ) or 'neither this or that' ( !( = || = ) )
Zdenek Krcal
49,910 PointsThank you guys. :-)
Raphaël Seguin
Full Stack JavaScript Techdegree Graduate 29,228 PointsRaphaël Seguin
Full Stack JavaScript Techdegree Graduate 29,228 PointsI think your if statement is wrong. The exception should be thrown if the char you're testing is neither a letter nor a $. So the logic should be :
!(Character.isLetter(l) || l = '$' )