Solutions to the Try It Out Exercises in Java For Dummies, 8th Edition
by Barry Burd

Chapter 13: Looking Good When Things Take Unexpected Turns

In this chapter:

Don't Be a Quitter

import java.util.Scanner; public class Main { public static void main(String[] args) { var keyboard = new Scanner(System.in); var words = new String[5]; int i = 0; try { do { words[i] = keyboard.next(); } while (!words[i++].equals("Quit")); } catch (ArrayIndexOutOfBoundsException e) { System.out.println(e.getMessage()); System.out.println("You can enter at most five words"); } for (int j = 0; j < 5; j++) { try { System.out.println(words[j].length()); } catch (NullPointerException e) { System.out.println(e.getMessage()); System.out.println("You could have entered up to five words"); } } keyboard.close(); } }

Loosen Up a Bit

public class OutOfRangeException extends RuntimeException { public OutOfRangeException(String message) { super("A value is out of range.\n" + message); } } class NumberTooLargeException extends OutOfRangeException { public NumberTooLargeException(String message) { super("A value is too large.\n" + message); } } import java.text.NumberFormat; import java.util.Scanner; import static java.lang.System.out; public class InventoryD { public static void main(String[] args) { double boxPrice = 0.0; int maxBoxes = 0; var keyboard = new Scanner(System.in); NumberFormat currency = NumberFormat.getCurrencyInstance(); try { out.print("What's the price per box? "); String boxPriceIn = keyboard.next(); boxPrice = Double.parseDouble(boxPriceIn); if (boxPrice < 0.0){ throw new OutOfRangeException("Price per box cannot be negative."); } out.print("What's the maximum number of boxes? "); String maxBoxesIn = keyboard.next(); maxBoxes = Integer.parseInt(maxBoxesIn); if (maxBoxes < 0){ throw new OutOfRangeException("Maximum number of boxes cannot be negative."); } out.print("How many boxes do we have? "); String numBoxesIn = keyboard.next(); int numBoxes = Integer.parseInt(numBoxesIn); if (numBoxes < 0) { throw new OutOfRangeException("You typed " + numBoxes + ". There's no such thing as a negative box."); } if (numBoxes > maxBoxes) { throw new NumberTooLargeException(numBoxes + " is larger than the maximum of " + maxBoxes); } out.print("The value is "); out.println(currency.format(numBoxes * boxPrice)); } catch (NumberFormatException e) { out.print(e.getMessage()); out.println(" ... Cannot interpret the input."); } catch (OutOfRangeException e) { out.println(e.getMessage()); } catch (Exception e) { out.println(e.getMessage()); } out.println("That's that."); keyboard.close(); } }

Reader's Acknowledgements

import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner diskScanner = null; try { diskScanner = new Scanner(new File("numbers.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } int[] numerators = new int[5]; int[] denominators = new int[5]; int i = 0; while (diskScanner.hasNextInt()) { try { numerators[i] = diskScanner.nextInt(); denominators[i] = diskScanner.nextInt(); } catch (IndexOutOfBoundsException e) { System.out.println("Array index " + i + " is out of bounds."); } catch (InputMismatchException e) { System.out.println ("Attempt to read a value that's not an integer"); } i++; } for (int j = 0; j < numerators.length; j++) { try { System.out.println(numerators[j] / denominators[j]); } catch (ArithmeticException e) { System.out.println("Divide by zero on array index " + i); } } diskScanner.close(); } }

If It's Broken, Fix It

import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Main { public static void main(String[] args) { var fileIn = new File("input"); FileInputStream fileInStrm = null; try { fileInStrm = new FileInputStream(fileIn); } catch (FileNotFoundException e1) { System.out.println(e1.getMessage()); } var dataInStrm = new DataInputStream(fileInStrm); var fileOut = new File("output"); FileOutputStream fileOutStrm = null; try { fileOutStrm = new FileOutputStream(fileOut); } catch (FileNotFoundException e1) { System.out.println(e1.getMessage()); } var dataOutStrm = new DataOutputStream(fileOutStrm); int numFilesCopied = 0; try { while (true) { dataOutStrm.writeByte(dataInStrm.readByte()); } } catch (EOFException e) { numFilesCopied = 1; } catch (IOException e) { System.out.println(e.getMessage()); } catch (NullPointerException e) { System.out.println(e.getMessage()); } System.out.println("Number of files copied: " + numFilesCopied); } }

Be Resourceful

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import static java.lang.System.out; public class DoPayroll { public static void main(String[] args) { out.println("Starting payroll ..."); cutCheck(); out.println("Payroll completed."); } public static void cutCheck() { try (var diskScanner = new Scanner(new File("EmployeeInfo.txt"))) { String name = diskScanner.nextLine(); double amountPaid = diskScanner.nextDouble(); out.printf("Pay to the order of %s: $%,.2f\n", name, amountPaid); } catch (FileNotFoundException e) { out.println(e.getMessage()); } } }

Don't Miss a Trick

import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.NoSuchElementException; import java.util.Scanner; import static java.lang.System.out; public class DoPayrollD { public static void main(String[] args) { out.println("Starting payroll ..."); try { cutCheck(); out.println("Payroll completed."); } catch (FileNotFoundException e) { out.println(e.getMessage()); } } public static void cutCheck() throws FileNotFoundException { try (var diskScanner = new Scanner(new File("EmployeeInfo.txt")); var diskScanner2 = new Scanner(new File("LegalInfo.txt"))) { String name = diskScanner.nextLine(); double amountPaid = diskScanner.nextDouble(); String disclaimer = diskScanner2.nextLine(); if (disclaimer.equals("")) { throw new NoSuchElementException(); } out.printf("Pay to the order of %s: $%,.2f\n", name, amountPaid); out.println(disclaimer); } catch (FileNotFoundException e) { out.println("Abnormal return from the cutCheck method ..."); throw e; } catch (InputMismatchException e) { out.println("Something is formatted incorrectly in the " + "EmployeeInfo.txt file."); } catch (NoSuchElementException e) { out.println("No text in the LegalInfo.txt file."); } } }