查找字符串中单词的长度,并查找有多少单词具有该长度。(爪哇)
我需要一些帮助来找出一个单词的长度以及有多少个单词有这个长度。例如,如果句子是"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();
}
}
这只是给出了每个单词的长度
# 1 楼答案
使用
string.split()
函数实际上很容易。我写信是为了演示一种解决方案:# 2 楼答案
你对我说的话毫无意义。我相信下面的东西对你有用
发出