dimanche 23 octobre 2016

Flesch-Kincaid Test (java)

I'm having trouble running my code. The problem seems to be at the switch (inputText.charAt(x)). It will not run and is causing me a huge headache. I'm a beginner so can someone please help me out?

package project_02;
import java.io.*;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class FKReadability {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        double index = 0.0;
        Scanner reader = new Scanner(System.in);
        String inputFileName;
        String inputText = "";
        int totalWords = 0, totalSentences = 0, totalSyllables = 0, OK;
        char syllableList [] = {'a', 'e', 'i', 'o', 'u'};
        boolean loop = true;
        if (loop == true){
       int i; i = JOptionPane.showConfirmDialog(null, "Would you like to run a text analysis?",
               "Start the Flesch-Kincaid Test",JOptionPane.YES_NO_OPTION);
        if (i ==JOptionPane.NO_OPTION)
        {
            JOptionPane.showMessageDialog(null, "The program will now exit. GoodBye.");
            System.exit(0);
        }
        if (i == JOptionPane.YES_OPTION)
        {
            System.out.println("What is the file's name you wish to read?");
            inputFileName = reader.next();

        // This will count one string at a time
        String line = "";

        try 
        {
            FileReader fileReader = 
                new FileReader(inputFileName);

            BufferedReader bufferedReader = 
                new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) != null) {
                 inputText = inputText + line;
                 System.out.println(line);
            }   

            // Always close files.
            bufferedReader.close();         
        }
        catch(FileNotFoundException ex) {
            inputText = "fedalitorugexa racecar axegurotiladef is a palindrome!";               
        }
        catch(IOException ex) {
            inputText = "fedalitorugexa racecar axegurotiladef is a palindrome!";               
        }
        // This function counts the word and sentences
        for (int j = 0; j < inputText.length(); j++)
        {
            switch (inputText.charAt(j))
            {
                case ' ':
                    totalWords++;
                case '.':
                    totalSentences++;
                case '!':
                    totalSentences++;
                case '?':
                    totalSentences++;
                case ';':
                    totalSentences++;
                case ':':
                    totalSentences++;

            }
        }
        for (int z = 0; z < syllableList.length; z++)
        {
            for (int x = 0; x < inputText.length(); x++)
            {

This is where the trouble begins

                switch (inputText.charAt(x))
                {
                    case 'e':
                        if (inputText.charAt(x+2) == syllableList[z] && inputText.charAt(x+2) != ' ')
                            totalSyllables--;
                        else if (inputText.charAt(x+1) == syllableList[z] && inputText.charAt(x+1) != ' ')
                            totalSyllables--;
                        else 
                            totalSyllables++;
                    case 'E':
                         if (inputText.charAt(x+2) == syllableList[z] && inputText.charAt(x+2) != ' ')
                            totalSyllables--;
                         else if (inputText.charAt(x+1) == syllableList[z] && inputText.charAt(x+1) != ' ')
                            totalSyllables--;
                         else
                             totalSyllables++;
                }
            }
            }
       }
        index = (.39 * totalWords/totalSentences) + (11.8 * totalSyllables/totalWords)-15.59;
        String gradeLevel = "";
        if (index >=12)
            gradeLevel = "12 grader/college student";
        if (index < 12 && index >= 11)
            gradeLevel = "11 grader";
        if (index < 11 && index >=10)
            gradeLevel = "10 grader";
        if (index < 10 && index >= 9)
            gradeLevel = "9 grader";
        if (index < 9 && index >= 8)
            gradeLevel = "8 grader";
        if (index < 8 && index >= 7)
            gradeLevel = "7 grader";
        if (index < 7 && index >= 6)
            gradeLevel = "6 grader";
        if (index < 6 && index >=5)
            gradeLevel = "5 grader";
        if (index < 5 && index >= 4)
            gradeLevel = "4 grader";
        if (index < 4 && index >= 8)
            gradeLevel = "3 grader";
        if (index < 3 && index >= 7)
            gradeLevel = "2 grader";
        if (index < 2 && index >= 6)
            gradeLevel = "1 grader";
        if (index < 1)
            gradeLevel = "Not in School";
        JOptionPane.showMessageDialog(null, "For the given text:\n"
                +inputText+"\nThe total number of sentences is: " 
                + totalSentences+ "\nThe total number of words is: " 
                + totalWords + "\nThe total number of syllables is: " 
                + totalSyllables 
                + "\n\nThe Flesch-Kincaid Readability Index is: " 
                + index + "\nThe related educational level is: " 
                + gradeLevel);
    i = JOptionPane.showConfirmDialog(null, "Would you like to run another text analysis?",
            "Start the Flesch-Kincaid Test",JOptionPane.YES_NO_OPTION);
        if (i == JOptionPane.NO_OPTION){
            loop = false; System.out.println("The program will now exit. Goodbye."); 
            System.exit(0);
        }
   }
  } 
}

Aucun commentaire:

Enregistrer un commentaire