Java:查找平均线和编号线

Java:查找平均线和编号线,java,regex,loops,java.util.scanner,Java,Regex,Loops,Java.util.scanner,我一直在试图找出如何在这组数据中找到所有年龄的平均值,并对每一行进行编号。到目前为止,我所做的只是让人无法理解。我的错误通常都是围绕着转换而来的,我被难倒在如何度过这个难关上 我真的很感激你的帮助 文本文件内容: 75 Fresco, Al 67 Dwyer, Barb 55 Turner, Paige 108 Peace, Warren 46 Richman, Mary A. 37 Ware, Crystal 83 Carr, Dusty 15 Sledd, Bob 64 Sutton, Ol

我一直在试图找出如何在这组数据中找到所有年龄的平均值,并对每一行进行编号。到目前为止,我所做的只是让人无法理解。我的错误通常都是围绕着转换而来的,我被难倒在如何度过这个难关上

我真的很感激你的帮助

文本文件内容:

75 Fresco, Al
67 Dwyer, Barb
55 Turner, Paige
108 Peace, Warren
46 Richman, Mary A.
37 Ware, Crystal
83 Carr, Dusty
15 Sledd, Bob
64 Sutton, Oliver
70 Mellow, Marsha
29 Case, Justin
35 Time, Justin
8 Shorts, Jim
20 Morris, Hugh
25 Vader, Ella
76 Bird, Earl E.
我工作的代码是:

import java.io.*;

public class Ex2 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File("people.txt"));
        while (input.hasNext()) 
        {
            String[] line = input.nextLine().replace(",", "").split("\\s+");
            String age = line[0];
            String lastName = line[1];
            String firstName = "";
            //take the rest of the input and add it to the last name
            for(int i = 2; 2 < line.length && i < line.length; i++)
                firstName += line[i] + " ";

            System.out.println(firstName + lastName + " " + age);
        }
    }
}
目标输出为:

Al Fresco 75
Barb Dwyer 67
Paige Turner 55
Warren Peace 108
Mary A. Richman 46
Crystal Ware 37
Dusty Carr 83
Bob Sledd 15
Oliver Sutton 64
Marsha Mellow 70
Justin Case 29
Justin Time 35
Jim Shorts 8
Hugh Morris 20
Ella Vader 25
Earl E. Bird 76
1. Al Fresco 75
2. Barb Dwyer 67
3. Paige Turner 55
4. Warren Peace 108
5. Mary A. Richman 46
...

The average age is: NUMBER

您可以使用以下代码。我对计算平均值的方法做了一些修改。不需要在while循环中使用额外的for循环。您可以计算定义两个临时变量的平均年龄。(
count
totalAge
)。然后,在循环中,应用程序可以增加计数以识别人数,在同一循环中,您可以将年龄添加到totalAge变量中。(请注意,年龄应按双倍计算)。完成循环后,您可以通过将totalAge除以count得到年龄

我注意到在您的代码平均打印部分中添加了while循环。这是不正确的,它应该在while循环之外。否则,程序将在每行之后打印平均行

通过下面的代码,我添加了内联注释

import java.io.*;
import java.util.Scanner;

public class Ex2 {
public static void main(String[] args) throws FileNotFoundException {
    Scanner input = new Scanner(new File("people.txt"));
    // to get number of people
    int count=0;
    //to get total age
    double totalAge=0;
    while (input.hasNext()) 
    {
        String[] line = input.nextLine().replace(",", "").split("\\s+");
        String age = line[0];
        String lastName = line[1];
        String firstName = "";
        //take the rest of the input and add it to the last name
        for(int i = 2; 2 < line.length && i < line.length; i++)
            firstName += line[i] + " ";

        //Convert age string into double and add it to totalAge
        totalAge=totalAge+Double.parseDouble(age);
        System.out.println(++count+" "+firstName + lastName + " " + age);
    }
    double average = totalAge / count;
    System.out.println("The average age is: " + average);
}
}

哦,天哪,这有很多方法可以处理

首先,在Java中,我们尝试将事物视为对象,在本例中,每一行实际上都是一个对象。要允许以后扩展和调整代码,请从

所以我们有了一个新的

Public Person(
    private int age;
    private String lastName;
    private Sring firstName;

    Public Person(int age, String lName, String fName){
       this.age = age;
       this.lastName = lname;
       this.firstName = fName;
    }
   //add getters here.
)
现在我们有了一个对象 我们可以创建一个函数

Array<Person> people = new ArrayList<>;
。。。 或者你可以用地图


你能提供文本文件内容吗?我的错误,补充了它。检查代码的每一行,自己描述一下你认为它的作用。同时也检查de注释。看看代码是否真的像你认为的那样。还要考虑你认为它的作用是否必要。巧合地停止编程,用一个计划开始。你需要把打印结束的代码移出循环。此外,我还将
sum
按我的方式计算人的年龄
totAge+=Integer.valueOf(第[0]行)
most引入了一个很好的新类,用于积累统计数据,如数据系列的统计数据。上面的代码未经测试,我不会使用它。如果您从对象而不是脚本行的角度来思考,它被设计为一个可实现的示例。
Public Person(
    private int age;
    private String lastName;
    private Sring firstName;

    Public Person(int age, String lName, String fName){
       this.age = age;
       this.lastName = lname;
       this.firstName = fName;
    }
   //add getters here.
)
Array<Person> people = new ArrayList<>;
String[] line = input.nextLine().replace(",", "").split("\\s+");
Person p = new Person(Integer.parseint(line[0]],line[1],line[2])
people.add(p);
Map<Integer,Person> people = new ListMap<>;
Integer total = 0
int i = 0
foreach(Person p: people){
   Integer total = total + p.getAge();
   i++
   System.out.Println(i + "." + p.getFirst() +" "+ p.getLast()+" "+ p.getAge())
}
Float average = Float.parseInt(total) / i;
System.out.Println("The average age is " + average);