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

Chapter 13: Looking Good When Things Take Unexpected Turns

In this chapter:

Add try-catch statements to keep the code from crashing

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String[] words = new String[5]; int i = 0; try { do { words[i] = keyboard.next(); } while (!words[i++].equals("Quit")); } catch (ArrayIndexOutOfBoundsException e) { 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("You could have entered up to five words"); } } keyboard.close(); } }

Read values (the price of each box and the number of boxes that's too large) from the user's keyboard

@SuppressWarnings("serial") class OutOfRangeException extends Exception { private String value; public OutOfRangeException(String value) { this.value = value; } public String getValue() { return value; } } @SuppressWarnings("serial") class NumberTooLargeException extends OutOfRangeException { private String value; public NumberTooLargeException(String value) { super(value); this.value = value; } public String getValue() { return value; } } import static java.lang.System.out; import java.util.Scanner; import java.text.NumberFormat; public class InventoryD { public static void main(String args[]) { Scanner keyboard = new Scanner(System.in); NumberFormat currency = NumberFormat.getCurrencyInstance(); try { out.print("What's the price per box? "); String boxPriceIn = keyboard.next(); double boxPrice = Double.parseDouble(boxPriceIn); if (boxPrice <= 0.0) { throw new OutOfRangeException(boxPriceIn); } out.print("What's the maximum number of boxes? "); String maxBoxesIn = keyboard.next(); int maxBoxes = Integer.parseInt(maxBoxesIn); if(maxBoxes <= 0) { throw new OutOfRangeException(maxBoxesIn); } out.print("How many boxes do we have? "); String numBoxesIn = keyboard.next(); int numBoxes = Integer.parseInt(numBoxesIn); if (numBoxes < 0) { throw new OutOfRangeException(numBoxesIn); } if (numBoxes > maxBoxes) { throw new NumberTooLargeException(numBoxesIn); } out.print("The value is "); out.println(currency.format(numBoxes * boxPrice)); } catch (NumberFormatException e) { out.print(e.getMessage()); out.println(" Cannot turn string into a number."); } catch (OutOfRangeException e) { out.print(e.getValue()); out.println("? That's impossible!"); } catch (Exception e) { out.print("Something went wrong, "); out.print("but I'm clueless about what "); out.println("it actually was."); } out.println("That's that."); keyboard.close(); } }

Fix the exceptions, including the unacknowledged FileNotFoundException

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(); } }

Fix the broken DataInputStream, DataOutputStream code

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) { File fileIn = new File("input"); FileInputStream fileInStrm = null; try { fileInStrm = new FileInputStream(fileIn); } catch (FileNotFoundException e1) { e1.printStackTrace(); } DataInputStream dataInStrm = new DataInputStream(fileInStrm); File fileOut = new File("output"); FileOutputStream fileOutStrm = null; try { fileOutStrm = new FileOutputStream(fileOut); } catch (FileNotFoundException e1) { e1.printStackTrace(); } DataOutputStream dataOutStrm = new DataOutputStream(fileOutStrm); int numFilesCopied = 0; try { while (true) { dataOutStrm.writeByte(dataInStrm.readByte()); } } catch (EOFException e) { numFilesCopied = 1; } catch (IOException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } System.out.println("Number of files copied: " + numFilesCopied); } }

Add close calls in finally blocks

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

Use a try-with-resources statement

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