import java.util.InputMismatchException; import java.util.NoSuchElementException; import java.util.Scanner; /** * This class implements a Java application that: * * Declares a Scanner variable. * * Declares an array of 26 String values, and initializes each array * element with the corresponding lower case letter of the alphabet. * * Prompts the user to enter an integer and uses the Scanner's nextInt * method to obtain that integer from the user. * * Prints out the corresponding letter from the array. * * Creates a new array of size 27, puts a string containing a space (" ") * at index 0 in the new array, and copies all the other letters to positions * 1..26 of the new array using System.arraycopy(). * * Loops through the array, printing each entry, either as is, or upper * case if it is a vowel ("a", "e", "i", "o", "u"). * * Prints the number of non-vowel strings at the end of the loop. * * The program catches the InputMismatchException from the call to nextInt, * in which case the program must display an appropriate error message and * end the program. * * The loop works for any size initial array, and not depend on the array * size. * * @author Jade Cheng * @assignment TA 1 part 1 * @date Jan 14, 2010 * @bugs None */ public class ChengJade1Part1 { /** * The entry point of the application. * * @param args The command line arguments (not used). */ public static void main(final String[] args) { // Declare a Scanner variable. final Scanner scanner = new Scanner(System.in); // Declare an array of of 26 String values, and initializes each array // element with the corresponding lower case letter of the alphabet. final String[] array = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; // Prompt the user to enter an integer and use the Scanner's nextInt // method to obtain that integer from the user. int input = 0; System.out.print("Please type an integer (0 to 25): "); // Catch the InputMismatchException from the call to nextInt, // in which case display an appropriate error message and // end the program. try { input = scanner.nextInt(); System.out.println(array[input]); } catch (final InputMismatchException e) { System.err.println("Input mismatch."); System.exit(1); } catch (final NoSuchElementException e) { System.err.println("\nInput stream closed."); System.exit(1); } catch (final ArrayIndexOutOfBoundsException e) { System.err.println("Index out of bounds."); System.exit(1); } // Create a new array of size 27, put a string containing a space // (" ") at index 0 in the new array, and copy all the other letters // to positions 1..26 of the new array using System.arraycopy(). final String[] newArray = new String[27]; newArray[0] = " "; System.arraycopy(array, 0, newArray, 1, 26); // Loop through the array, print each entry, either as is, or upper // case if it is a vowel ("a", "e", "i", "o", "u"). int consonants = 0; for (int i = 0; i < newArray.length; i++) { if ("aeiou".indexOf(newArray[i]) >= 0) { System.out.print(newArray[i].toUpperCase()); continue; } // NOTE: This is an alternative way to check for a vowel: // // if (newArray[i].equals("a") || // newArray[i].equals("e") || // newArray[i].equals("i") || // newArray[i].equals("o") || // newArray[i].equals("u")) { // System.out.print(newArray[i].toUpperCase()); // continue; // } System.out.print(newArray[i]); consonants++; } // Print the number of non-vowel strings at the end of the loop. System.out.println("\nNumber of consonants: " + consonants); // Exit cleanly while debugging from Eclipse. System.exit(0); } }