更新:在每个人和一些朋友的帮助下弄明白!我需要捕获字符串变量中的\ n字符以及空字符串。感谢所有评论和帮助过的人!
这是针对Java简介的作业,我们还没有得到方法,当谷歌搜索帮助时,这是唯一突然出现的东西,所以我想我也可以尝试自问。
提前致谢!
对于我们的项目,我们必须创建一个计算一些简单文本的FRI的程序,我遇到两个问题。
第一个问题是,在使用Scanner对象和while循环计算文件中的句子时,我的计数总是错误+1。我可以在控制台中看到打印出它正在计算空字符串,\ n打印输出(我打印到控制台所以我可以看到给出的变量,而不是因为它是必需的),即使我有它如果我的字符串不为空,则设置为仅增加累积变量。
任何帮助将不胜感激! 我不确定我的代码需要哪些部分,但是......
/* Count Sentences */
//Scanner object to read inputText
Scanner countSentences = new Scanner(inputText);
// While loop to count sentences
while(countSentences.hasNext()){
String sentence = countSentences.useDelimiter(SENTENCE_DELIMITERS).next();
System.out.println(sentence);
if(sentence.compareTo("") != 0){
numSentences++;
}
}
...是计算句子的位置。
再次感谢!
整个代码!编辑: ////////////////////////////////
描述: 用于计算文本的Flesch可读性指数的程序 * /
public class Readability {
public static void main(String[] args) throws IOException{
final double FIRST_CONSTANT = -1.015; //First constant of FRI formula
final double SECOND_CONSTANT = 84.6; //Second constant of FRI formula
final double THIRD_COSTANT = 206.835; //Third constant of FRI formula
final String SENTENCE_DELIMITERS = "[.:;?!]"; //Sentence delimiters
final String WORD_DELIMITERS = "[.:;?! ,\t,\n]"; //Word delimiters
int numSentences = 0; //Number of sentences in inputText
int numWords = 0; //Number of words in inputText
int numSyllables = 0; //Number of syllables in each word in inputText
String standardText = "This is just a test."; //Standard Text for testing
String inputText = ""; //Holds the text being tested currently
/* Ask user if they want to run a text analysis */
int choice = JOptionPane.showConfirmDialog(null, "Do you want to run a text analysis?", "Start the Flesch Reading Ease Test", JOptionPane.YES_NO_OPTION);
/* Message Displayed if 'No' selected, and program exits */
if(choice == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "The program exits. Good Bye!");
System.exit(0);
}
/* If user selects 'Yes' */
// Ask user if they have a text file or wish to use the standard text
choice = JOptionPane.showConfirmDialog(null, "Answer \"Yes\" if you want to read the text from a file!\nAnswer \"No\" if you want a test run on a standard text.", "Select the input!", JOptionPane.YES_NO_OPTION);
/* If user selects 'Yes' */
if(choice == JOptionPane.YES_OPTION){
// Ask user for filename
String inputFileName = JOptionPane.showInputDialog("Enter input file name with extension!\nInclude the path if file is not in the project folder!");
// User presses cancel, set inputText to standardText
if(inputFileName == null){
inputText = standardText;
}
// User presses enter, create file object using user input as filename
else{
File file = new File(inputFileName);
// Check if file exists, if file exists create scanner object to read text from file
if(file.exists()){
Scanner readFile = new Scanner(file);
// Read the file line by line and collect the concatenated lines in the variable inputText
while(readFile.hasNext()){
String fileSentences = readFile.nextLine();
inputText += fileSentences + "\n";
}
}
// If file doesn't exist, set inputText to the standardText
else {
inputText = standardText;
}
}
}
/* If user selects 'No' */
else{
inputText = standardText;
}
/* Count Sentences */
//Scanner object to read inputText
Scanner countSentences = new Scanner(inputText);
// While loop to count sentences
while(countSentences.hasNext()){
String sentence = countSentences.useDelimiter(SENTENCE_DELIMITERS).next();
System.out.println(sentence);
if(sentence.length() != 0){
numSentences++;
}
System.out.println(numSentences); //This println prints the correct number to the console
}
System.out.println(numSentences); //This println prints +1
//Close countSentences scanner
countSentences.close();
/* Count Words */
//Scanner object to read inputText
Scanner countWords = new Scanner(inputText);
// While loop to count words and increment syllable count per word (rule #3: each word has at least one syllable)
while(countWords.hasNext()){
String word = countWords.useDelimiter(WORD_DELIMITERS).next();
if(word.compareTo("") != 0){
numWords++;
//numSyllables++;
}
// For loop to count syllables as each word is read
for(int k = 0; k < word.length(); k++){
char letter = word.charAt(k);
}
}
//Test//
System.out.println(inputText);
System.out.println(numSentences);
System.out.println(numWords);
System.out.println(numSyllables);
}
}
答案 0 :(得分:0)
这是工作计划。它给了我2的结果。
import java.util.Scanner;
public class Scannerd {
public static String inputText = "What!? That is awful!";
public static String SENTENCE_DELIMITERS = "[.:;?!]";
public static void main(String... arg) {
int numSentences = 0;
Scanner countSentences = new Scanner(inputText);
// While loop to count sentences
while (countSentences.hasNext()) {
String sentence = countSentences.useDelimiter(SENTENCE_DELIMITERS).next();
System.out.println(sentence);
if(sentence.compareTo("") != 0){
numSentences++;
}
}
System.out.println("#ofSentences"+numSentences);
}
}
答案 1 :(得分:0)
看起来它在开头使用默认分隔符,因为自定义分隔符在循环内使用。 试试这个。
import java.util.Scanner; 公共类TestScanner {
public static String inputText =“什么!?太糟糕了!”;
public static String SENTENCE_DELIMITERS =“[。:;?!]”;
public static void main(String... arg) {
int numSentences = 0;
Scanner countSentences = new Scanner(inputText);
countSentences.useDelimiter(SENTENCE_DELIMITERS);
// While loop to count sentences
while (countSentences.hasNext()) {
//String sentence = countSentences.useDelimiter(SENTENCE_DELIMITERS).next();
String sentence = countSentences.next();
System.out.println(sentence);
if(sentence.compareTo("") != 0){
numSentences++;
}
}
System.out.println("#ofSentences"+numSentences);
}
}
答案 2 :(得分:0)
尝试使用hasNextLine()和nextLine()方法。
/* Count Sentences */
//Scanner object to read inputText
Scanner countSentences = new Scanner(inputText);
// While loop to count sentences
while(countSentences.hasNextLine()){
String sentence = countSentences.useDelimiter(SENTENCE_DELIMITERS).nextLine();
System.out.println(sentence);
if(sentence.compareTo("") != 0){
numSentences++;
}
}