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 trialOraseanu Daniel
1,278 PointsHow can I execute both return and throw statements?
Hi, This exercise requires to execute return (for first step) and throw an exception (step 2) in the same method. Is this possible? because both actions force the method to exit. If so, how would that be possible?
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 = normalizeDiscountCode(discountCode);
}
private String normalizeDiscountCode(String discountCode){
String cod ="";
for(char l : discountCode.toCharArray()){
if (!Character.isLetter(l) || l !='$'){
throw new IllegalArgumentException ("Invalid discount code");
} else {
cod += l;
}
}
return cod.toUpperCase();
}
}
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"
}
}
}
3 Answers
Joseph Wasden
20,407 PointsYes, given normalizeDiscountCode("w@w"), the method is interrupted, or in other words will throw an exception, and therefore will not continue on to execute the return statement. If an exception had not been thrown, then the method would continue the method logic without throwing an exception and therefore reach the return statement portion. If it throws an exception before reaching the return statement, the method will break and not continue on to executing the return statement.
This behavior fits with the request of the second part of the code challenge, "In the normalizeDiscountCode verify that only letters or the $ character are used. If any other character is used, throw a IllegalArgumentException with the message Invalid discount code."
Joseph Wasden
20,407 PointsThe short answer is that yes, it can contain the way to have both a return value and have to ability to throw errors. This is accomplished through the use of for statements to iterate and conditionals to determine if the exception should be thrown. Your if statement logic is close, but it should read as AND (&&) not OR (||) to capture the range of exceptions desired.
public class Order {
private String itemName;
private int priceInCents;
private String discountCode;
private String normalizeDiscountCode(String discountCode) {
char[] array = discountCode.toCharArray();
for (char letter : array) {
if (letter != '$' && ! Character.isLetter(letter)){
throw new IllegalArgumentException("Invalid discount code");
}
}
return discountCode.toUpperCase();
}
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 = normalizeDiscountCode(discountCode);
}
}
Donnie Turner
2,063 PointsRemember that we are testing "!+" NOT EQUAL here, which if it were true, would execute the IllegalArgumentException. If it is false, meaning the person entered a letter or $ symbol, then it would skip the IAE and go to the return statement. I have below the code I used to complete the exercise which I believe to be simpler and more in the spirit of what Dennis taught in this lesson as follows.
private String normalizeDiscountCode(String discountCode) {
for (char letter : discountCode.toCharArray()) {
if (letter != '$' && ! Character.isLetter(letter)){
throw new IllegalArgumentException("Invalid discount code");
}
}
return discountCode.toUpperCase();
}
This
Oraseanu Daniel
1,278 PointsThanks Joseph,
I tried your code and works but I still don't understand how the return statement can be executed in case of " normalizeDiscountCode("w@w")" . In this case, " throw new IllegalArgumentException("Invalid discount code"); " is executed and the method is interrupted, right?
Oraseanu Daniel
1,278 PointsOraseanu Daniel
1,278 PointsThanks for taking the time to help me!