|
|
| java help |
Expert:Maurice
Code a try statement that calls the calculateDiscount method and assigns the return value to a double variable named discountPct. If an IllegalArgumentException occurs, the message contained in the exception object should be displayed at the console. Regardless of whether an IllegalArgumentException occurs, print the message “calculateDiscount method called.”
|
| Answers |
Hi friend,
Code to catch the IllegalArgumentException occurs :
import java.io.*; public class CalDiscount { public static void main(String[] args) throws IOException {
double calculateDiscount = calculateDiscount(); System.out.println("calculateDiscount : " + calculateDiscount); }
public static double calculateDiscount() throws IOException { double discountPct=0; InputStreamReader isr = new InputStreamReader(System.in); BufferedReader bufReader = new BufferedReader(isr); System.out.println("Enter the value "); try { discountPct = Double.parseDouble(bufReader.readLine()); } catch(IllegalArgumentException e) { System.out.println("calculateDiscount method called."); }
return discountPct; } }
For more information on Exception Handling in Java visit to :
http://www.roseindia.net/java/exceptions/
Thanks
|
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|