【发布时间】:2013-11-01 05:12:03
【问题描述】:
我需要一些帮助来查找单词的长度以及有多少单词具有该长度。例如,如果句子是"I am going to find some string lengths",则
输出将是
Number of String with length 1 is 1
Number of String with length 2 is 2
Number of String with length 4 is 2
Number of String with length 5 is 1
Number of String with length 6 is 1
Number of String with length 7 is 1
到目前为止,我得到了这个:
String word;
int wordlength;
int count = 0;
Scanner inFile =
new Scanner(new FileReader("C:\\Users\\Matt\\Documents\\WordSize.txt\\"));
PrintWriter outFile =
new PrintWriter("wordsizes.out");
while (inFile.hasNext())
{
word = inFile.next();
wordlength = word.length();
if (count >= 0)
outFile.println(wordlength);
count++;
}
outFile.close();
}
}
这只是给出了每个单词的长度。
【问题讨论】:
-
您的预期输出实际上没有意义,至少对我而言。无论如何,您面临的问题是什么?你还没有真正问过问题。
-
问题和代码对我来说都没有意义! :(
-
对不起各位!我试图找到一种方法来清楚地解释它,但让我再试一次。我需要找到字符串中每个单词的长度。找到每个单词的长度后,我需要列出每个长度有多少单词。所以如果有 3 个 5 个字母的单词和 2 个 6 个字母的单词,我需要它说出来。
-
@Matt,未删除答案。抱歉我没空。
标签: java string count word string-length