Assignemnt #86 and Letter At A Time
Code
/// Name: Brian Phillips
/// Period: 5
/// Program name: LetterAtATime.java
/// File Name: Letter at a Time
/// Date Finished: 3/4/2016
import java.util.Scanner;
public class LetterAtATime
{
public static void main( String[] args )
{
// 1. Adding an = after < gives an error message because it is one character farther than the message
// 2. Length of box is 3. The last position is 2
// 3. Same reason as number 1 it is because it is one character past the actual message
// 4. Ok I did it
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");
//Adding an = after < gives an error message because it is one character farther than the message
for ( int i=0; imessage.length(); i++ )
{
System.out.println("\t" + i + " - '" + message.charAt(i) + "'");
}
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. Wow");
System.out.println("\nYour message contains the letter 'e' " + e_count + " times. Wow");
System.out.println("\nYour message contains the letter 'i' " + i_count + " times. Wow");
System.out.println("\nYour message contains the letter 'o' " + o_count + " times. Wow");
System.out.println("\nYour message contains the letter 'u' " + u_count + " times. Wow");
}
}
Picture of the output