简体   繁体   English

如何对 Java 中的文本文件的行进行排序?

[英]How do I sort lines of a text file in Java?

I have a text file with hockey players names and stats, and I'm trying to sort their stats from greatest to least.我有一个包含曲棍球运动员姓名和统计数据的文本文件,我正在尝试将他们的统计数据从大到小排序。 (Text file: https://i.stack.imgur.com/H8sVg.png ) (文本文件: https://i.stack.imgur.com/H8sVg.png

I'm having trouble placing the players stats into a 2D-array我在将球员统计数据放入二维数组时遇到问题

Here is my code so far:到目前为止,这是我的代码:

import java.util.*; 
import java.io.*; 
public class Main { 
  public static void main(String[] args) throws Exception{
Scanner in = new Scanner(new File("mapleleafscoring.txt~~"));
int[][] myArr = new int[20][8];

for(int r=0; r<myArr.length; r++) {
    for(int c=0; c<myArr[r].length; c++) {
        for (int i = 0; i <= 1; i++) {
          myArr[r][c]=in.next();
        }
      for (int j = 0; j <= 4; j++) {
      myArr[r][c]=in.nextInt();
        }
      myArr[r][c]=in.nextDouble();
        }
    }

    System.out.println("Player Name        GP  G  A  P  S   S%");
    Scanner choice = new Scanner(System.in); 
    System.out.println("Would you like to sort by Games Played, Goals, Assists, Points, Shots, or Shot percentage?"); 
    String sort = choice.next(); 
    if (sort.equalsIgnoreCase("goals")){
      Arrays.sort(myArr, (a, b) -> Integer.compare(a[0], b[3]));;
    } 
    else if (sort.equalsIgnoreCase("games played")){
      Arrays.sort(myArr, (a, b) -> Integer.compare(a[0], b[2]));
    } 
    else if (sort.equalsIgnoreCase("assists")){
      Arrays.sort(myArr, (a, b) -> Integer.compare(a[0], b[4]));
    } 
    else if (sort.equalsIgnoreCase("points")){
      Arrays.sort(myArr, (a, b) -> Integer.compare(a[0], b[5]));
    } 
    else if (sort.equalsIgnoreCase("shots")){
      Arrays.sort(myArr, (a, b) -> Integer.compare(a[0], b[6]));
    } 
    else if (sort.equalsIgnoreCase("shot percentage")){
      Arrays.sort(myArr, (a, b) -> Double.compare(a[0], b[7]));
    } 
    else {System.out.println("Invalid input");} 
    in.close(); 
    choice.close();
    } 
}

in the nested for loop that you are reading, you keep overwriting the same index over and over again.在您正在阅读的嵌套 for 循环中,您一遍又一遍地覆盖相同的索引。

your code:你的代码:

for(int r=0; r<myArr.length; r++) { //lets say r = 0
  for(int c=0; c<myArr[r].length; c++) { //lets say c = 1
    for (int i = 0; i <= 1; i++) {
      myArr[r][c]= in.next(); //myArr[0][1] getting the value of in.next() twice
    }
    for (int j = 0; j <= 4; j++) {
      myArr[r][c]=in.nextInt(); //myArr[0][1] getting the value of in.next() 5 times
    }
      myArr[r][c]=in.nextDouble(); //myArr[0][1] finally ending up with the value read on this line.
  }
}

I would reather:我想:

for(int r=0; r<myArr.length; r++) { //lets say r = 0
  for(int c=0; c<myArr[r].length; c++) { //lets say c = 1
    if(!in.hasNext()) break; //safeguard for broken data.
    if (c <= 1) {
      myArr[r][c]= in.next(); //myArr[0][1] get's the read done here. 
    } else if(c <= 6) {
      myArr[r][c]=in.nextInt(); 
    } else {
      myArr[r][c]=in.nextDouble(); 
    }
  }
}

now, this program will work strictly for data set of size 20 x 8. if you rather want to dynamically read the file, I would suggest using LinkedList<LinkedList<Object>> (or of type String) as your 2D array.现在,该程序将严格适用于大小为 20 x 8 的数据集。如果您想动态读取文件,我建议您使用LinkedList<LinkedList<Object>> (或字符串类型)作为二维数组。 while reading the file, I would read on line at a time using i.nextLine() .在阅读文件时,我会使用i.nextLine()一次在线阅读。 then, I would split each line by de-limiter (perhaps space).然后,我会用分隔符(可能是空格)分割每一行。

refer to oracle docs on scanner to identify capabilities that you can leverage: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html请参阅扫描仪上的 oracle 文档以确定您可以利用的功能: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM