Results 1 to 2 of 2
  1. #1
    L_22 is offline Member
    Join Date
    Mar 2008
    Rep Power

    Default Help with Flesch Index


    I need to calculate the Flesch Index of a given extract.
    To do this I have done a Flesch Index class as below but it does not count my words,sentences and syllables.Please can anyone assist in what I am doing incorrect.

    import javax.swing.*;
    import java.util.StringTokenizer;

    class FleschIndex

    String extract;

    public void setExtract(String ext)



    public String getExtract()

    return extract;


    public int readabilityIndex(String extract)
    String delimiters = ".,':;?{}[]=-+_!@#$%^&*() ";
    StringTokenizer token = new StringTokenizer(extract,delimiters);
    String word;
    int numberOfSyllables = 0;
    int numberOfSentences = 0;
    int numberOfWords = 0;
    int CalculatedIndex;

    //count the words
    String wordDelimiter=" ";
    StringTokenizer wordTokenizer=new StringTokenizer(extract,wordDelimiter);

    //count the sentences
    String sentenceDelimiters=".:;?!";
    StringTokenizer sentenceTokenizer = new StringTokenizer(extract,sentenceDelimiters);
    numberOfSentences= sentenceTokenizer.countTokens();

    //count syllables

    while (token.hasMoreTokens())

    word = token.nextToken();

    for(int x=0; x<word.length();x++)

    char letter=word.charAt(x);//first loop checks first letter,second loop checks second letter....




    char endLetter;
    endLetter=word.charAt(word.indexOf(" ")-1);
    if ((endLetter == 'e') && (numberOfSyllables != 1))




    return CalculatedIndex;


    public String educationLevel(int CalculatedIndex)

    String educationLevel="";

    if (CalculatedIndex<0)
    {educationLevel="Law School Graduate";}

    else if (CalculatedIndex>=0&&CalculatedIndex<=30)
    {educationLevel="College Graduate";}

    else if ((CalculatedIndex>30)&&(CalculatedIndex<=50))
    {educationLevel="College Student";}

    else if (CalculatedIndex>50&&CalculatedIndex<=60)
    {educationLevel="High School Student";}

    else if (CalculatedIndex>60&&CalculatedIndex<=66)
    {educationLevel="9th Grader";}

    else if (CalculatedIndex>66&&CalculatedIndex<=70)
    {educationLevel="8th Grader";}

    else if (CalculatedIndex>70&&CalculatedIndex<=80)
    {educationLevel="7th Grader";}

    else if (CalculatedIndex>80&&CalculatedIndex<=90)
    {educationLevel="6th Grader";}

    else if ((CalculatedIndex>90)&&(CalculatedIndex<=100))
    {educationLevel="5th Grader";}

    return educationLevel;


    This is my Run class:
    class Run
    public static void main (String [] args)

    FleschIndex FI=new FleschIndex ();

    FI.setExtract("The string tokenizer class allows an application to break a string into tokens.Hello.");
    System.out.println("The extract is:"+FI.getExtract());

    int CalculatedIndex=FI.readabilityIndex(FI.getExtract( ));
    System.out.println("The Flesch Index is:"+FI.readabilityIndex(FI.getExtract()));
    System.out.println("The education level is:" +FI.educationLevel(CalculatedIndex));


  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Rep Power


    C:\jexp>java FleschIndex
    The extract is:The string tokenizer class allows an application to break a string into tok
    ens. Hello.
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of
    range: -2
    at java.lang.String.charAt(Unknown Source)
    at FleschIndex.readabilityIndex(
    at FleschIndex.main(
    Java Code:
            char endLetter;
            // If word.indexOf(" ") = -1
            // then word.indexOf(" ")-1 will evaluate to -2.
            //endLetter=word.charAt(word.indexOf(" ")-1);
            // Try:
            endLetter = word.charAt(word.length()-1);

Similar Threads

  1. Replacing at an index
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-29-2008, 07:33 AM
  2. how to load a servlet on index.jsp page...
    By sujithc34 in forum Java Servlet
    Replies: 1
    Last Post: 01-19-2008, 01:28 AM
  3. z-Index problem
    By mjdousti in forum AWT / Swing
    Replies: 1
    Last Post: 12-29-2007, 02:34 AM
  4. problems with array index
    By mary in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:30 PM
  5. Accessing index.dat files
    By vissu007 in forum Networking
    Replies: 1
    Last Post: 07-01-2007, 04:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts