删除从文件读入的字符串中的特定字符和所有整数。从字符串中删除int,char
目标是擦除正在通过扫描仪读取的字符串中的所有非句子结束字符和数字。删除这些字符和整数的原因是从读入的单词中生成准确的单词数,句数和音节数。
原始发布的代码非常粗糙,已被修复,并将被重新编译从我以下发布。
谢谢你的所有时间和帮助!
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);
}
}
不明白你的问题,但'next.charAt(J)== NUMS [I]'这里你用一个char' '1' 比较一个int == 1'我认为并非如此 – nachokk
使用'Integer.parseInt()'静态方法将文本转换为int。 –
next = next.replace(next.substring(j,j),“”); –