Assignemnt #86: LetterAtATime
Code
/// Name: Kelsey Lieberman
/// Period 5
/// Program Name: LetterAtATime
/// File Name: LetterAtATime.java
/// Date Finished: 1/7/2016
import java.util.Scanner;
public class LetterAtATime
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("\nWhat is your message? ");
String str = keyboard.nextLine();
int length = str.length() ;
System.out.println("\nYour message is " + length + " characters long.");
System.out.println("The first character is at position 0 and is '" + str.charAt(0) + "'.");
System.out.println("The last character is at position " + (length - 1) + " and is '" + str.charAt(length - 1) + "'.");
System.out.println("Here are all the characters, one letter at a time.");
for ( int i=0; i < length; i++ )
{
System.out.println("\t" + i + " - '" + str.charAt(i) + "'");
}
int a_count = 0;
for ( int i=0; i < length; i++ )
{
char letter = str.charAt(i);
if ( letter == 'a' || letter == 'A' || letter == 'e' || letter == 'E' || letter == 'i' || letter == 'I' || letter == 'o' || letter == 'O' || letter == 'u' || letter == 'U' )
{
a_count++;
}
}
System.out.println("\nYour message contains vowels " + a_count + " times. Isn't that interesting?");
}
}
/// if you use <= the program will not run correctly because the character count starts at 0, whereas the length of the message starts its count at 1, so the length will always be 1 character greater than the number of characters, even though technically they are the same number, they just start from different points.
///if a string variable contains the word, "box", the length of the word is 3, but the last character, x, is at position 2.
///because length is going to be one longer than the position of the last charater in a string.
Picture of the output