Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-31-2008, 05:00 PM
Member
 
Join Date: Mar 2008
Posts: 1
L_22 is on a distinguished road
Help with Flesch Index
Hi,

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)
{

extract=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);
numberOfWords=wordTokenizer.countTokens();



//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....
if((letter=='a')||
(letter=='e')||
(letter=='i')||
(letter=='o')||
(letter=='u')||
(letter=='y'))

{

numberOfSyllables++;

}

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

numberOfSyllables--;

}
}


}



CalculatedIndex=(int)Math.round(206.835-((84.6)*((numberOfSyllables))/(numberOfWords))-((1.015)*((numberOfWords)/(numberOfSentences))));
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));


}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-31-2008, 06:30 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Quote:
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(FleschIndex.java:59)
at FleschIndex.main(FleschIndex.java:114)
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);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 09:40 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org