Assignment #86 A Letter at a Time
Code
///Name: Ian Grant
///Period:5
///Project Name: A Letter at A Time
///File Name:LetterTime.java
///01/27/16
import java.util.Scanner;
public class LetterTime
{
public static void main( String[] args )
{
/*
1. It gets a error because the length is one more than the last position
2. Length of box is 3. The last position is 2
3. It can't be equal to the length because the length is one more than the last position
4.
*/
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) + "'");
}
int a_count = 0;
int e_count = 0;
int i_count = 0;
int o_count = 0;
int u_count = 0;
for ( int i=0; i< message.length(); i++ )
{
char letter = message.charAt(i);
if ( letter == 'a' || letter == 'A' )
{
a_count++;
}
if ( letter == 'e' || letter == 'E' )
{
e_count++;
}
if ( letter == 'i' || letter == 'I' )
{
i_count++;
}
if ( letter == 'o' || letter == 'O' )
{
o_count++;
}
if ( letter == 'u' || letter == 'U' )
{
u_count++;
}
}
System.out.println("\nYour message contains the letter 'a' " + a_count + " times. Isn't that interesting?");
System.out.println("\nYour message contains the letter 'e' " + e_count + " times. Isn't that interesting?");
System.out.println("\nYour message contains the letter 'i' " + i_count + " times. Isn't that interesting?");
System.out.println("\nYour message contains the letter 'o' " + o_count + " times. Isn't that interesting?");
System.out.println("\nYour message contains the letter 'u' " + u_count + " times. Isn't that interesting?");
}
}
Output