java - 来自文本文件的数据评估

标签 java arrays file sorting text

我需要一些帮助。我不知道如何解决我的问题。 我有这种格式的文本文件:

personx 25
personz 2  
persony 5  
persony 7  
personi 55  
personx 25

我需要计算每个人的人数。输出应为“personx = 50”等
我无法使用我知道有 10 人的旧系统。所以我有 10 个变量,我只是用扫描仪浏览了文件,检查行是否以“personx”开头,然后计算变量 personx 等的数量。我现在不想使用这些变量。我不想在每个新人加入后都更改代码。
怎么解决这个问题呢?我想让这个输出从最高到最低排序:

personi = 55
personx = 50
persony = 12
personz = 2

不使用变量是否可能

personi, personx, persony, personz ?
My idea was to go through the file and scan the names. Save the name into an array and add another name into an array if that name is not in the array yet. So I will have the names.
Next I will scan the file again and check for the name and number. Check name + number and then save the number into another array on the same possition as the name in the first array. So I will have

Names[0] = "personi";
Count[0] = 55;
Names[1] = "personx";
Count[1] = 50;

然后我将用 for 循环打印这两个数组。

我认为这不是最好的解决方案。你会怎么做?有更好/更快/更简单的东西吗?以及如何解决排序问题?
感谢您的时间。

最佳答案

您可以我们Map<String,Integer>

在本例中我使用了TreeMap这将为您整理一切。如果您不需要排序,则只需使用 HashMap而不是TreeMap .

Map<String, Integer> map = new TreeMap();
  try {
      BufferedReader reader = new BufferedReader(new FileReader(new File("C:/iPhone/persons.txt")));
      String line = "";
      String [] person = new String[2];
      while ((line = reader.readLine()) != null) {
          person = line.split(" ");
          String name = person[0];
          int number = Integer.parseInt(person[1]);
          map.put(name,map.getOrDefault(name,0)+number);
      }
      reader.close();
  } catch (IOException ioe) {
      ioe.printStackTrace();
  }
  map.forEach((k,v)->System.out.println(k + " = " + v));

}

persons.txt:

personx 25
personz 2  
persony 5  
persony 7  
personi 55  
personx 25

输出:

personi = 55
personx = 50
persony = 12
personz = 2

1) 我可以在文件中使用这个,其中行不是我的格式,但它有例如..这种格式? “人物 bla bla 25”?是否也可以转换呢?怎么办?

是的,您可以创建为您完成此操作的方法。您可以使用字符串分割或一些正则表达式。

2)为什么会有String[] person = new String[2]; ?

错误,应该是String[1] 。现已更正

3) 什么是 String line = ""; ?

这是新的String我存储从文件中读取的每一行的位置。如您所见,我分配 reder.readLine()在 while 循环中。之后我就把它分开了。

编辑: 更改了代码,以便人员可以有多个参数,但仅将第一个作为名称,最后一个作为数字。

public static void main(String[] args) {
        Map<String, Integer> map = new TreeMap();
          try {
              BufferedReader reader = new BufferedReader(new FileReader(new File("C:/iPhone/persons.txt")));
              String line = "";
              String [] person;
              while ((line = reader.readLine()) != null) {
                  person = line.split(" ");
                  String name = person[0];
                  int number = Integer.parseInt(person[person.length-1]);
                  map.put(name,map.getOrDefault(name,0)+number);
              }
              reader.close();
          } catch (IOException ioe) {
              ioe.printStackTrace();
          }
          map.forEach((k,v)->System.out.println(k + " = " + v));
        }

persons.txt:

personx  asdasdas dasd asd a 25
personz 2  
persony asd asd asd a 5  
persony 7  
personi 55  
personx 25

输出:

personi = 55
personx = 50
persony = 12
personz = 2

关于java - 来自文本文件的数据评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38862536/

相关文章:

java - 使用java套接字从客户端向服务器发送文本文件

java - ElasticSearch 高级 API 使用自定义设置创建索引返回错误

java - Hudson/Jenkins vs Cruise Control vs Luntbuild vs Continuum

ios - Swift 3 - 如何从 super View 以及数组中删除所有 subview

c - 转发声明到指向结构的指针数组搞砸了?

iphone - NSURLConnection 下载大文件(>40MB)

java - GWT WebApp 寻找入口点

java - 哪个 Java 库用于从连接的摄像机录制视频?

php - 如何在PHP中将具有不同内容的两个数组结果链接起来?

python - (Python) 如何自动移动创建的文件?