Download presentation
Presentation is loading. Please wait.
1
Handle Exceptions
2
Murach’s Java SE 6, C5© 2007, Mike Murach & Associates, Inc. Slide 2 What is an Exception?
3
Murach’s Java SE 6, C5© 2007, Mike Murach & Associates, Inc. Slide 3 Scanner class’ next methods may throw inputMitsmatchException if the data entered is not the expected data type. For example, nextDouble method gets text data.
4
Example of Exception Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:840) at java.util.Scanner.next(Scanner.java:1461) at java.util.Scanner.nextDouble(Scanner.java:2387) at testinstall.Main.main(Main.java:26) Java Result: 1 BUILD SUCCESSFUL (total time: 10 seconds)
5
Murach’s Java SE 6, C5© 2007, Mike Murach & Associates, Inc. Slide 5 Using Try / Catch Statements to Handle Exceptions
6
Try Catch: Typically work with a loop structure Scanner sc=new Scanner(System.in); try{ System.out.println("enter loan:"); double loan=sc.nextDouble(); System.out.println("enter rate in %:"); double rate=sc.nextDouble(); System.out.println("enter term in year:"); double term=sc.nextDouble(); double monPayment=(loan*rate/100/12)/(1-(Math.pow(1+rate/100/12,-12*term))); System.out.println("Monthly payment is: =" + monPayment); } catch (InputMismatchException e) { sc.next(); System.out.println("Enter numeric data only"); } System.out.println("Thank you for using payment calculator!");
7
Loan Calculator with Exception Handling Scanner sc=new Scanner(System.in); String flag="Y"; while (flag.equalsIgnoreCase("Y")){ try{ System.out.println("enter loan:"); double loan=sc.nextDouble(); System.out.println("enter rate in %:"); double rate=sc.nextDouble(); System.out.println("enter term in year:"); double term=sc.nextDouble(); double monPayment=(loan*rate/100/12)/(1-(Math.pow(1+rate/100/12,-12*term))); System.out.println("Monthly payment is: =" + monPayment); System.out.println("Do you want to continue? (Y/N):"); flag=sc.next(); } catch (InputMismatchException e) { sc.next(); System.out.println("Enter numeric data only"); continue; } System.out.println("Thank you for using payment calculator!");
8
Methods of Exception Class catch (InputMismatchException e) catch (Exception e) – Catch any exceptions System.out.println(e.getMessage()); System.out.println(e.getStackTrace());
9
String Class Methods toLowerCase() toUpperCase() substring(int beginIndex) – "unhappy".substring(2) returns "happy" – 0-based index substring(int beginIndex, int endIndex) – "hamburger".substring(4, 8) returns "urge" "smiles".substring(1, 5) returns "mile" length() indexOf( char)
10
Full name format: First Name + Space + Last Name Change the Full Name to proper format where the first letter of First Name and Last Name changed to uppercase and other letters in lower case Example: “david chao” -> “David Chao”
11
Code Example public static void main(String[] args) { // TODO code application logic here Scanner sc=new Scanner(System.in); System.out.println("enter full name:"); String FullName=sc.nextLine(); int spaceIndex = FullName.indexOf(" "); String firstName=FullName.substring(0,spaceIndex); String lastName=FullName.substring(spaceIndex+1); String newFullName=proper(firstName) + " " + proper(lastName); System.out.println("Proepr full name:" + newFullName); } public static String proper(String Name){ return Name.substring(0,1).toUpperCase()+ Name.substring(1).toLowerCase(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.