Assignment # 86 and Letter at a time
Code
/// Name: Tommy Oyuntseren
/// Period: 7
/// Program Name: Letter
/// File Name: Letter.java
/// Date: 3/1/2016
///if the for loop is changed to i<=message.length() the programs stops after listing the characters and displays a an exception message that the String index is out of range
///if the String contains "box" the length is three and the position of the letter "x" is two
/// the for loop repeat as long as i < message.length() instead of i <= message.length() because the value of i as to be less than the message for the characters to be found in a position and if it's equal it doesn't have a position
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 vowels = 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' )
{
vowels++;
}
}
System.out.println("\nYour message contains " + vowels + " vowels. Isn't that interesting?");
}
}
Picture of the output