import java.util.InputMismatchException;
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 Cheng Yu
* @assignment TA 1 part 1
* @date Aug 29, 2008
* @bugs None
*/
public class ChengJade01Part1 {
/**
* 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 error.");
return;
} catch (final ArrayIndexOutOfBoundsException e) {
System.err.println("too large.");
}
// Check to see if the input index is too large.
if (input >= array.length)
System.err.println("The index is too large.");
// Print out the corresponding letter from the array.
// else
// System.out.println(array[input]);
// 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);
}
}