Flesch Readability Index Code
Hey, I'm writing a program about the Flesch Readability Index that is supposed to calculate the number or words, syllables, and sentences in a file. I have gotten the words counter to work, but I'm having a little difficulty with the syllables. The file I'm testing with just states "These cats are fast.", but I'm not getting a syllable count of 4. Hope you can help me figure this out! ^^
Code Java:
//count syllables; each group of adjacent vowels is one syllable; regal would be 2
//vowels = a,e,i,o,u,y
//e at end of word is not a vowel
//each word has at least one syllable
//sentence = ends by ., :, ;, ?, or !
//index is computed by Index = 206.835 - 84.6*(num of syll/num of words) - 1.015*(num of words/num of sent)
//rounded to nearest int
//read file, compute index, print out educational level
import java.io.*;
import javax.swing.JFileChooser;
import java.util.Scanner;
public class Flesch
{
public static void main (String [] args) throws IOException
{
File datafile = null;
JFileChooser chooser = new JFileChooser();
FileReader reader = null;
String input = "";
String [] words;
if(chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
datafile =chooser.getSelectedFile();
Scanner in = new Scanner(new FileReader(datafile));
String content = in.nextLine();
int totalWords = 0;
int totalSyll = 0;
//WORDS
for (int i=0; i<content.length(); i++)
{
if (content.charAt(i)==' ')
totalWords++;
else if (content.charAt(i) == '.')
totalWords++;
else if (content.charAt(i) == ':')
totalWords++;
else if (content.charAt(i) == ';')
totalWords++;
else if (content.charAt(i) == '?')
totalWords++;
else if (content.charAt(i) == '!')
totalWords++;
else
totalWords=totalWords;
}
System.out.println("There are " + totalWords + " words.");
char syllableArray [] = {'a','e','i','o','u','y'};
//SYLLABLES
for (int k=0; k<syllableArray.length; k++)
{
for (int i=0; i<=content.length(); i++)
{
if (content.charAt(i) == syllableArray[k] & content.charAt(i) == syllableArray[k])
totalSyll=totalSyll;
if (content.charAt(i) == syllableArray[1]+' ')
totalSyll=totalSyll;
if (content.charAt(i) == syllableArray[k])
totalSyll++;
else
totalSyll=totalSyll;
}
}
System.out.println("There are "+ totalSyll + " syllables.");
}
}
Re: Flesch Readability Index Code
Quote:
'm not getting a syllable count of 4.
What count are you getting?
What is your logic/algorithm for finding syllables?
Re: Flesch Readability Index Code
I'm getting an Out Of Bounds Exception error, so I know somewhere either the int i or the int k is getting to be too high (probably off by one) with their increment operators, so they access any more, because there's nothing to access.
The logic I was using that if a word ends in 'e', then that isn't a syllable, so I put if it ends in 'e' and then there's white space ' ' then the total number of syllables stays the same. If a word has 2 vowels next to each other, that isn't considered a vowel either. But the only part I"m having trouble with the out of bounds exception error, I"m not sure where it's coming from.
Re: Flesch Readability Index Code
Quote:
getting an Out Of Bounds Exception error
Please post the full text of the error message. It will have the line number and the value of the index.
Look at the size of the array and compare it to the value of the index in the error message.
Remember: that indexes start at 0. The max index is the length of the array -1.
Re: Flesch Readability Index Code
Oh, I got it. In my for loops, I made sure that the variables i and k couldn't equal the number of indices, but I didn't realize that the increment operator would make them one higher. I fixed it by adding minus one to both of the for loops. Thank you for the advice! ^^