-1

I am writing a program that is supposed to calculate the Flesch index of a passage or string and tell the minimum education level required to comprehend.

What is supposed to happen is a person types in a string that the want to be calculated and the program is supposed to count the number of words and count the number of sentences as well as go through each word and calculate how many syllables there are in the word and add that to the total syllable counter. The way to calculate syllables is to count the number of adjacent vowels in a word, so "Computer" for instance has the vowels o, u, and e so 3 syllables. But, if the word ends in the letter e, subtract 1 from the total syllable count so, "Passage" has a, a, and e, but since the e is at the end, the syllable count is 2.

Here is what I got so far:

// Import scanner to get input from the user
import java.util.Scanner;

public class Flesch
{ 
  public static void main (String [] args)
  {
    public static final double BASE = 206.835;
    public static final double WORD_LENGTH_PENALTY = 84.6;
    public static final double SENTENCE_LENGTH_PENALTY = 1.015;

    Scanner input = new Scanner (System.in);

    // Declare variables for syllables and words
    int totalWords = 0;
    int totalSyllables = 0;

    // Prompt user for a passage of text
    System.out.println ("Please enter some text:");
    String passage = input.nextLine();

    // Words
    for (int i = 0; i < passage.length(); i++)
    {
      if (passage.charAt(i) == ' ') {
        totalWords++;
      }
      else if (passage.charAt(i) == '.') {
        totalWords++;
      }
      else if (passage.charAt(i) == ':') {
        totalWords++;
      }
      else if (passage.charAt(i) == ';') {
        totalWords++;
      }
      else if (passage.charAt(i) == '?') {
        totalWords++;
      }
      else if (passage.charAt(i) == '!') {
        totalWords++;
      }
    } 



    System.out.println ("There are " + totalWords + " words.");

    char syllableList [] = {'a', 'e', 'i', 'o', 'u', 'y'};

    // Syllables
    for (int k = 0; k < syllableList.length; k++)
    {
      for (int i = 0; i < passage.length(); i++)
      {
        if (passage.charAt(i) == syllableList[k]) {
          totalSyllables = totalSyllables + 1;
        } 
        if (lastChar(passage) == 'E' || lastChar(passage) == 'e') {
          totalSyllables = totalSyllables - 1;
        } else {
          break;
        }
      }
    }    
    System.out.println ("There are " + totalSyllables + " syllables.");
    input.close();

    public static char lastChar (String aWord)
    {
      char aChar = aWord.charAt(aWord.length() - 2);
      System.out.print (aChar);
      return aChar;
    }

    /** Return true if the last character in the parameter word is
      * a period, question mark, or exclaimation point, and false
      * otherwise
      **/
    public static boolean sentenceEnd (String word)
    {
      if (lastChar(passage) == '.') {
        return true;
      } else if (lastChar(passage) == '?') {
        return true;
      } else if (lastChar(passage) == '!') {
        return true;
      } else {
        return false;
      }
    }

    double fleschIndex = BASE - WORD_LENGTH_PENALTY * (totalSyllables / totalWords) - SENTENCE_LENGTH_PENALTY * (totalWords / totalSentences);

    if (fleschIndex > 100) {
      educationLevel = "4th grader";
    } else if (fleschIndex <= 100) {
      educationLevel = "5th grader";
    } else if (fleschIndex <= 90) {
      educationLevel = "6th grader";
    } else if (fleschIndex <= 80) {
      educationLevel = "7th grader";
    } else if (fleschIndex <= 70) {
      educationLevel = "8th grader";
    } else if (fleschIndex <= 60) {
      educationLevel = "9th grader";
    } else if (fleschIndex <= 50) {
      educationLevel = "high school graduate";
    } else if (fleschIndex <= 30) {
      educationLevel = "college graduate";
    } else if (fleschIndex < 0) {
      educationLevel = "law school graduate";
    }

    System.out.println ("The Flesch idex is " + fleschIndex + ".");
    System.out.println ("The text can be understood by a " + educationLevel + ".");
  }
} 

I keep getting some weird error messages telling me to put semi-colons around the boolean declarations which makes no sense to me.

Kara
  • 6,115
  • 16
  • 50
  • 57
  • Apparently you are not familiar with basic Java syntax. And by "basic" I really mean basic. Grab a book of the sorts of "Writing your very first Java program", as someone fixing the errors you have here is useless both for the site and for you. – Ordous Sep 24 '14 at 15:22
  • possible duplicate of [Flesch Index program Java](http://stackoverflow.com/questions/25984531/flesch-index-program-java) – Willem Van Onsem Apr 07 '15 at 22:48
  • Can some moderator delete this question ? – vissu Feb 15 '17 at 05:15

1 Answers1

0

It looks to me like you mixed up your methods.

The methods sentenceEnd and lastChar are inside the main-method. This is not allowed. They have to be outside of the main-method but inside the Flesch-Class.

quadrrem
  • 21
  • 2