【发布时间】:2013-08-31 00:55:56
【问题描述】:
从文件读取的字符串中删除特定字符和所有整数。
目标是从正在通过扫描仪读取的字符串中清除所有非句子结尾字符和数字。删除这些字符和整数的原因是为了从读入的单词中生成准确的字数、句子数和音节数。
原贴的代码很粗糙,已经修复,下面我自己重新贴一下。
感谢您在这方面的所有时间和帮助!
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]在这里您将 int 与 char'1' == 1进行比较,我认为这不是真的 -
使用
Integer.parseInt()静态方法将文本转换为int。 -
next = next.replace(next.substring(j,j), "");
-
@nachokk 编辑了上面的问题。
-
“我收到一条错误消息,提示我无法用字符串替换整数。” - 更具体一些。这是编译错误消息吗?它出现在哪条线上。 确切地说是什么意思。
标签: java arrays string methods