Source of MoreCrashes.java


  1: import java.util.Scanner;
  2: import java.util.NoSuchElementException;

  4: /**
  5:  * A program that crashes.  Fix one problem, another arises!
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class MoreCrashes {

 11:     public static void main(String[] args) {
 12:         System.out.println("\n\n"
 13:                 + "This program crashes a lot!\n");

 15:         // ...because this array is shorter than it needs to be
 16:         int[] numbers = new int[2];
 17:         numbers[0] = 1;
 18:         numbers[1] = 2;
 19:         numbers[2] = 3;
 20:         System.out.println(numbers[0] + ", " + numbers[1]
 21:                 + ", " + numbers[2]);

 23:         // ...because this String variable isn't referring to a String
 24:         String name = null;
 25:         System.out.println("Here is your name: " + name);
 26:         System.out.println("It has " + name.length() + " letters in it.");
 27:         System.out.println("Its first letter is " + name.charAt(0));

 29:         // ...if the user types in less than 5 numbers on the line
 30:         Scanner kbd = new Scanner(System.in);
 31:         System.out.println("Enter four numbers here: ");
 32:         Scanner line = new Scanner(kbd.nextLine());
 33:         for (int i = 1; i <= 5; i++) {
 34:             System.out.println("The " + i + "th number you entered was "
 35:                     + line.nextInt());
 36:         }
 37:     }
 38: }