在java中,如何通过初始字符计算平均术语长度?
我有一个包含一段文本的文件(txt),我必须用java中的初始字符计算平均术语长度。例如,如果有4个单词“紫色、棕色、粉色、黑色”,则输出应为; p=5 b=5 我想了好几个小时,但还是做不到 我所能做的就是把话一分为二。我不知道怎样才能写出一系列具有相同首字母的单词在java中,如何通过初始字符计算平均术语长度?,java,Java,我有一个包含一段文本的文件(txt),我必须用java中的初始字符计算平均术语长度。例如,如果有4个单词“紫色、棕色、粉色、黑色”,则输出应为; p=5 b=5 我想了好几个小时,但还是做不到 我所能做的就是把话一分为二。我不知道怎样才能写出一系列具有相同首字母的单词 try(Scanner scanner = new Scanner(new FileReader("sampleText.txt"))) { ArrayList<String&g
try(Scanner scanner = new Scanner(new FileReader("sampleText.txt"))) {
ArrayList<String> arrlist = new ArrayList<String>();
while (scanner.hasNext()) {
String sampleText = scanner.nextLine();
String[] array = sampleText.split(" ");
for (int i = 0; i < array.length; i++) {
if (array[i].charAt(0) = Integer ) //tried to not take numbers didn't work
System.out.println(array[i]);
arrlist.add(array[i]);
}
System.out.print(array);
break;
}
}
catch (FileNotFoundException e) {
System.out.println("FNF Exception");
}
catch (ArrayIndexOutOfBoundsException a) {
System.out.println("AIOoB Exception");
}
}
try(Scanner Scanner=new Scanner(new FileReader(“sampleText.txt”)){
ArrayList arrlist=新的ArrayList();
while(scanner.hasNext()){
String sampleText=scanner.nextLine();
String[]数组=sampleText.split(“”);
for(int i=0;i
如果允许使用Java Streams,则可以使用收集器解决此任务:
:按初始字符分组groupingBy
:保留文字mappingBy
:计算单词的平均长度averaging
也可以使用<代码>过滤器>代码>只考虑“有效”字(仅包含字母)
静态映射GetAverageLength(字符串行){
返回GetAverageLength(Arrays.stream(line.split(\\s+)));
}
静态映射GetAverageLength(流字){
返回words.filter(w->w.matches(“[A-Za-z]+”)//包含至少1个字母的单词
.collect(收集器.groupingBy(w->w.charAt)(0),
Collectors.mapping(w->w,Collectors.averaging(String::length));
}
测试:
System.out.println(GetAverageLength(
asList(“紫色”、“棕色”、“粉色”、“黑色”、“天蓝”、“123”、“$abc”).stream()
));
System.out.println(GetAverageLength(“橘子苹果和杏仁是开胃菜”);
输出:
{p=5.0, b=5.0, a=5.0}
{a=5.4, o=6.0}
下面是如何根据每个单词的第一个字符对其进行分组
Map<Character, List<String>> = new HashMap<>();
for (String word : array) {
List<Integer> l;
Character c = word.charAt(0);
if (m.containsKey(c)) {
l = m.get(c);
} else {
l = new ArrayList<>();
m.put(c, l);
}
l.add(word.length());
}
Map=newhashmap();
for(字符串字:数组){
清单l;
字符c=单词字符(0);
如果(m.containsKey(c)){
l=m.get(c);
}否则{
l=新的ArrayList();
m、 put(c,l);
}
l、 添加(word.length());
}
现在,您有了一个从该字符开始的字符到字长的映射,您可以对该字符求平均值,您可以通过以下方式进行求解:
,\\s+
),将生成的字符串存储到列表中,然后对列表进行排序
Map
存储计算数据,其中键将是初始字符,值将是平均术语长度import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
String str = "purple, brown, pink, black";
// Split the string and store into a List<String>
List<String> list = Arrays.asList(str.split(",\\s+"));
// Sort the list
list.sort(Comparator.naturalOrder());
// Use a Map<String, Integer> to store the data
Map<Character, Double> map = new LinkedHashMap<>();
for (int i = 0; i < list.size(); i++) {
// Get the element and move to the loop counter to the next index
String element = list.get(i++);
double sum = element.length();
Character ch = Character.valueOf(element.charAt(0));
int count = 1;
// Starting with the next index, iterate the list until you find an element with
// a different initial letter
while (i < list.size() && ch.equals(Character.valueOf(list.get(i).charAt(0)))) {
// Add the length of element and move the outer loop counter to the next index
sum += list.get(i++).length();
count++;
}
map.put(ch, sum / count);
// Adjust the the outer loop counter by decrementing by 1
i--;
}
// Display the result
System.out.println(map);
}
}
{b=5.0, p=5.0}
如果我能很好地理解他,
5
和5
这两个词总共有10个字符。@Meninx-メネンックス 是的,你是对的。尝试使用地图,然后你可以根据第一个字符对每个单词进行分组