Assignment #86: Letter At a Time

Code

import java.util.Scanner;

public class LetterAtATime
{
	public static void main( String[] args )
	{
		Scanner kb = new Scanner(System.in);

		System.out.print("What is your message? ");
		String message = kb.nextLine();

		System.out.println("\nYour message is " + message.length() + " characters long.");
		System.out.println("The first character is at position 0 and is '" + message.charAt(0) + "'.");
		int lastpos = message.length() - 1;
		System.out.println("The last character is at position " + lastpos + " and is '" + message.charAt(lastpos) + "'.");
		System.out.println("\nHere are all the characters, one at a time:\n");

		for ( int i=0; i<message.length(); i++ )
		{
			System.out.println("\t" + i + " - '" + message.charAt(i) + "'");
		}
        
        //If i is set to be <= message.length(), the code throws an exception stating that the last character is "out of range".

		int vowel_count = 0;

		for ( int i=0; i<message.length(); i++ )
		{
			char letter = message.charAt(i);
			if ( letter == 'a' || letter == 'A' letter == 'e' || letter == 'E' letter == 'i' || letter == 'I' letter == 'o' || letter == 'O' letter == 'u' || letter == 'U' )
			{
				vowel_count++;
			}
		}

		System.out.println("\nYour message contains the letter 'a' " + vowel_count + " times. Isn't that interesting?");

	}
}

//The length of the string "box" is 3, but the letter "x" is at position 2.

//The character counter counts up from zero, but the length function counts up from 1, so the script must stop early or it will try to count a character that is not there.
    

Picture of the output