从文件中读取的字符串中删除特定字符和所有整数。
目标是从通过扫描仪读取的字符串中清除所有非句子结尾字符和数字。删除这些字符和整数的原因是为了从读入的单词中生成准确的字数、句子数和音节数。
原贴的代码很粗糙,已经修复,下面我自己重新贴一下。
感谢您为此付出的所有时间和帮助!
public class Word {
private int wordCount, sentenceCount, syllableCount;
private int nums [] = {1,2,3,4,5,6,7,8,9,0};
private char vowels [] = {'a', 'e', 'i', 'o', 'u', 'y'};;
private char punctuation [] = {'!', '?','.', ';', ':','-','"','(', ')'};;
public Word()
{
wordCount = 0;
sentenceCount = 0;
syllableCount =0;
}
public Word(String next)
{
if(next.length() > 1)
{
//
// Remove punctuation that does not create sentences
//
for(int i = 5; i < punctuation.length; i++)
for(int j = 0; j < next.length(); j++)
if(next.charAt(j) == punctuation[i])
next = next.replace(j, "");
//
// Counting Sentences
//
for(int i=0; i < 5; i++)
if(punctuation[i] == next.charAt(next.length()-1))
sentenceCount++;
//
// Remove numbers for accurate word counting
//
for(int i = 0; i < nums.length; i++)
for(int j = 0; j < next.length(); j++)
if(next.charAt(j) == nums[i])
next = next.replace(j, "");
//
// Counts all syllables
//
for(int i = 0; i < vowels.length; i++)
for(int j = 0; j < next.length()-1; j++)
if(vowels[i] == next.charAt(j))
syllableCount++;
System.out.println(next);
}
}