java - Flesch-Kincaid 测试 (java)

标签 java testing flesch-kincaid

我在运行我的代码时遇到问题。问题似乎出在开关 (inputText.charAt(x)) 上。它不会运行,让我非常头疼。我是初学者,有人可以帮助我吗?

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 = 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);
            }
        }
    } 
}

最佳答案

您的情况似乎没有正确提供输入文件。我用项目文件夹根目录下的输入文件尝试了相同的代码,它成功了。

关于java - Flesch-Kincaid 测试 (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40209625/

相关文章:

testing - Cypress - cmd 运行的不可见元素

javascript - 使用 Javascript 计算段落中的音节

php - Flesch-Kincaid 可读性 : Improve PHP function

java - 使用 Jsoup 搜索雅虎问答

java - 我该如何处理这个错误使其正确 ?"invalid method declaration; return type required"

javascript - 如何在ajax url中传递多个数组值

angular - Angular Testing 中 tick() 和 flush() 有什么区别?

java - 类型安全优先规则

xml - 模式 (xsd) 等效性的回归测试