我在运行代码时遇到问题。问题似乎是在开关(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);
}
}
}
}
答案 0 :(得分:0)
您的情况似乎未正确提供输入文件。我在项目文件夹的根目录下使用输入文件尝试了相同的代码,并且可以正常工作。