Assignment #86 and Letter at a Time
Code
//Name: Mark Katz
///Period: 6
///Program Name: Letter at a Time
///File Name: letter.java
///Date Finished: 11/10/15
import java.util.Scanner;
public class letter
{
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 v_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' )
{
v_count++;
}
}
System.out.println("\nYour message contains " + v_count + " vowels. Isn't that interesting?");
}
}
// if you change the '<' to a '<=" then after the last letter there is nothign there so powershell won't be able to read it.
// The length will be 2 and x would be in the second position
//because it starts a 0, not 1, so the length will be 12 instead of 13
Picture of the output