Assignment #86
Code
/// Nmae: Jonathan Stine
/// Period: 5
/// Program Name: Letter at a Time
/// File Name: LetterAtATime.java
/// Date Finished: What is time really?
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 a_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' )
{
a_count++;
}
}
System.out.println("\nYour message contains a vowel " + a_count + " times. Isn't that interesting?");
}
}
/// 1. Making it <= creates an error when it tries to go beyond its range.
/// 2. The length is 3 and the position of "x" is at 2.
/// 3. In the box example for instance, the length is 3 so it would run until line 3. However, the "x" is on line 2 and is the end, so, the 3rd line has nothing to print.
Code Output