我需要一些帮助来找到一个单词的长度以及有多少单词有这个长度。例如,如果句子是"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();
}
}
这只是给出了每个单词的长度。