Source of DoesntCatchInputMismatch.java


  2: import java.util.Scanner;

  4: /**
  5:  * This program crashes if the user types the wrong kind of data.
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class DoesntCatchInputMismatch {

 11:     public static void main(String[] args) {
 12:         Scanner kbd = new Scanner(System.in);

 14:         // introduce yourself
 15:         System.out.print("\n\n"
 16:                 + "IO Exceptions\n"
 17:                 + "-------------\n"
 18:                 + "\n"
 19:                 + "This program crashes if the user types the wrong kind "
 20:                 + "of value.\n");
 21:         System.out.print("\n"
 22:                 + "Enter numbers below and I will calculate their sum. "
 23:                 + "Enter a negative number to\nend your list.\n\n");

 25:         // sum up a bunch of numbers
 26:         double sum = 0.0;
 27:         System.out.print("Enter a number: ");
 28:         double num = kbd.nextDouble();
 29:         kbd.nextLine();
 30:         while (num >= 0.0) {
 31:             sum += num;
 32:             System.out.print("Enter another number: ");
 33:             num = kbd.nextDouble();
 34:             kbd.nextLine();
 35:         }

 37:         // report the result
 38:         System.out.println("The sum of the numbers you entered was " + sum);
 39:         System.out.print("\n\n");
 40:     }
 41: }