我在尝试平均 java 中地图的值时遇到了很多麻烦。我的方法接受一个文本文件,并查看以某个字母开头的每个单词的平均长度(不区分大小写并遍历文本文件中的所有单词。
例如,假设我有一个包含以下内容的文本文件:
"Apple arrow are very common Because bees behave Cant you come home"
Run Code Online (Sandbox Code Playgroud)
我的方法目前返回:
{A=5, a=8, B=7, b=10, c=10, C=5, v=4, h=4, y=3}
Run Code Online (Sandbox Code Playgroud)
因为它是看字母,求单词的平均长度,但是还是区分大小写的。
它应该返回:
{A=5, a=8, B=7, b=10, c=10, C=5, v=4, h=4, y=3}
{a=4.3, b=5.5, c=5.0, v=4.0, h=4.0, y=3}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止。
public static Map<String, Integer> findAverageLength(String filename) {
Map<String, Integer> wordcount = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
try
{
Scanner in = new Scanner(new File(filename));
List<String> wordList = new ArrayList<>();
while (in.hasNext())
{
wordList.add(in.next());
}
wordcount = wordList.stream().collect(Collectors.toConcurrentMap(w->w.substring(0,1), w -> …Run Code Online (Sandbox Code Playgroud) My method would read from a text file and find the word "the" inside of each line and count how many lines contain the word. My method does work but the issue is that I need only lines that contain the word by itself, not a substring of the word as well
For example, I wouldn't want "therefore" even though it contains "the" it's not by itself.
I'm trying to find a way to limit the lines to those that …