|
|
| java help |
Expert:Maurice
Code a try statement that parses a string variable named quantityString to an int variable and then calls the calculateDiscount method and assigns the return value to a double variable named discountPct. Code a catch block for a NumberFormatException that prints the message “Invalid integer” to the console. Don’t code a catch block for the IllegalArgumentException. Write code that prints the message “Try statement completed” to the console regardless of whether a NumberFormatException or an IllegalArgumentException occurs. And write code that prints the message “No illegal argument” if a NumberFormatException occurs, but not if an IllegalArgumentException occurs.
|
| Answers |
Hi friend,
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()); System.out.println("Try statement completed"); } catch(NumberFormatException e) { System.out.println("Invalid integer."); }
return discountPct; } }
Thanks
|
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|