| Xerxes1986 2005-11-23, 3:44 pm |
| Hi everyone. I am taking a Java class right now and one of the assignments is giving me alot of trouble. Too much trouble. The assignment was to create a program that takes input, and analyzes it counting the words, characters, vowels, consonants, letters, digits, punctuation, etc. Also it had us count how many of each letter and digit occured. She said she will be testing the file by doing "java TextStats < testfile" where testfile is a file containing the input.
My problem is that when i run the program, it works flawlessly. But if i try making a testfile and using her syntax to test it, it throws out an error that:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at TextStats.main(TextStats.java:62)
Here is my source code:
code:
//File: TextStats.java
//Description: This program will analyze text that the user enters via the keybo
ard. It will count the number of words, characters, letters, vowels, consonants,
digits, and punctuation. It will also count the number of times each letter and
digit occurs in the input. It will display all of the results to the screen aft
erwards.
//Programmer: John Doe
//Date: 11/18/2005
import java.util.*;
import java.util.Scanner;
/**
Description: Program 5 for CS 119, Fall 2005
@author John Doe
*/
public class TextStats
{
/**
Main method starts here
*/
public static void main(String[] args)
{
//Initialize the variables
Scanner kbd = new Scanner(System.in);
int words = 0;
int characters = 0;
int vowels = 0;
int consonants = 0;
int digits[] = new int[9];
int punctuation = 0;
int letters[] = new int['z'];
int other = 0;
String input;
char tempchar;
int numlets = 0;
int numdigits = 0;
//Initialize the values in the arrays
for(int i=0; i < digits.length; i++)
digits[i] = 0;
for(char let='a'; let <= 'z'; let++)
{
letters[let - 'a'] = 0;
// System.out.println(let + " " + letters[let - 'a']);
}
/**
if(isVowel('h'))
System.out.println("It is a vowel");
if(isPunctuation('?'))
System.out.println("It is punctuation");
*/
//Start gathering the data and analyzing it
System.out.println("Enter some data to be analyzed. Type CTRL+D
to stop the input");
do
{
input = kbd.next();
words++;
for(int i=0; i < input.length(); i++)
{
tempchar = input.charAt(i);
characters++;
if (Character.isDigit(tempchar))
{
digits[tempchar - '0']++;
numdigits++;
}
else if (Character.isLetter(tempchar))
{
tempchar = Character.toLowerCase(tempcha
r);
if (isVowel(tempchar))
vowels++;
else
consonants++;
letters[tempchar - 'a']++;
numlets++;
}
else if (isPunctuation(tempchar))
punctuation++;
else other++;
}
} while(kbd.hasNext());
//Output the results
System.out.println("The input had the following attributes:");
System.out.println("Words: " + words);
System.out.println("Characters: " + characters);
System.out.println("Letters: " + numlets);
System.out.println("Vowels: " +vowels);
System.out.println("Consonants: " +consonants);
System.out.println("Digits: " +numdigits);
System.out.println("Punctuation: " + punctuation);
System.out.println("Other charachters: " + other);
System.out.println("\nBreakdown of the letters:");
for(char let='a'; let <= 'z'; let++)
System.out.println(let + " " + letters[let - 'a']);
System.out.println("\nBreakdown of the digits:");
for(int i=0; i < digits.length; i++)
System.out.println(i + " " + digits[i]);
}
//Function that uses a switch statement to determine if the argument is
a vowel or not.
public static boolean isVowel(char ch)
{
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return true;
default:
return false;
}
}
//Function that uses a switch statement to determine if the argument is
punctuation or not.
public static boolean isPunctuation(char ch)
{
switch(ch)
{
case '.':
case ',':
case ';':
case '?':
case '!':
case ':':
case '\"':
case ''':
return true;
default:
return false;
}
}
}
|