Assignemnt #86 and Letter At A Time
Code
///Name: Daniel Tiffany-Appleton
///Period: 7
///Program Name: Letter At A Time
///File Name: LetterAtATime
///Date Finished: 2/8/16
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) + "'");
}
int vowel_count = 0;
//What happened was that the program crash or end because the string index was out of bounds.
for ( int i=0; i < message.length(); i++ )
{
char letter = message.charAt(i);
if ( letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u' )
{
vowel_count++;
}
else if ( letter == 'A' || letter == 'E' || letter == 'I' || letter == 'O' || letter == 'U' )
{
vowel_count++;
}
}
//The length of "box" is 2 and the position of x is 2
//The for loop repeats that long because the word ends with the last letter, so the for loop has to end before the end of the word.
System.out.println("\nYour message contains " + vowel_count + " vowels. Isn't that interesting?");
}
}
Picture of the output