简体   繁体   English

程序在尝试对双精度进行排序时终止

[英]Program terminates while trying to sort doubles

I'm trying to create a method where it reads doubles from my .txt file which looks like: 我正在尝试创建一种方法,该方法从我的.txt文件中读取双精度数据,如下所示:

Homer Simpson, 50.0
Zoidberg, 100
Peter Griffin, 34.0
Lisa Simpson, 100

and sort them in descending order, here's my code: 并按降序对其进行排序,这是我的代码:

public static void sortGrade() throws IOException {
    FileReader reader = new FileReader("Grades.txt");
    BufferedReader buffer = new BufferedReader(reader);
    Scanner input = new Scanner ("Grades.txt");
    Double dGrade=0.0;
    ArrayList<Double> grade = new ArrayList<Double>();

        while (input.hasNextDouble()) 
        {
            grade.add(dGrade);
        }
    reader.close();
    Collections.sort(grade, Collections.reverseOrder());
    FileWriter fileWriter = new FileWriter("Grades.txt");
    PrintWriter out = new PrintWriter(fileWriter);
    for (Double outputLine : grade) 
    {
        out.println(outputLine);
    }

    out.close();
    }
}

After I call the method, it deletes my .txt file and terminates the program. 调用该方法后,它将删除我的.txt文件并终止程序。 Does anyone know what i'm doing wrong syntactically or logically? 有谁知道我在语法上或逻辑上做错了什么?

You have several problems in your code: 您的代码中有几个问题:

  1. You declare BufferedReader buffer = new BufferedReader(reader); 您声明BufferedReader buffer = new BufferedReader(reader); but never use the buffer to read the data, instead you use the Scanner input . 但请勿使用buffer读取数据,而应使用Scanner input

  2. Scanner input = new Scanner ("Grades.txt"); uses Scanner(String) which means it will use the String parameter as the source to read the data. 使用Scanner(String) ,这意味着它将使用String参数作为读取数据的源。 You should pass it as a File instead, like this: 您应该改为将其作为File传递,如下所示:

     Scanner input = new Scanner(new File("Grades.txt")); 
  3. You're creating an output file with the same name and path of the input file, noted here: 您正在创建具有与输入文件相同名称和路径的输出文件,在此处注明:

     FileWriter fileWriter = new FileWriter("Grades.txt"); 

    Use a different name and location for this file, like: 为此文件使用其他名称和位置,例如:

     FileWriter fileWriter = new FileWriter("Grades-out.txt"); 

    In case you want/need to append data to the end of the output, then use FileWriter(String, boolean) and pass the second parameter as true . 如果您希望/需要数据附加到输出的末尾,请使用FileWriter(String, boolean)并将第二个参数传递为true

     FileWriter fileWriter = new FileWriter("Grades-out.txt"); 

    Be aware that when you use this approach you have to manually clear the file before executing the application, otherwise you may have duplicated data in your input. 请注意,使用这种方法时,必须在执行应用程序之前手动清除文件,否则输入中的数据可能重复。

  4. From 2, since you haven't read any double from "Gradex.txt" string, then there's no output in the file, so the current output file, Grades.txt, will be an empty file. 从2开始,由于您尚未从"Gradex.txt"字符串中读取任何double "Gradex.txt" ,因此该文件中没有输出,因此当前输出文件Grades.txt将为空文件。

  5. I recommend you to create a class called Person where you store both the name string and the double (whatever it means), then store every instance of Person in a List<Person> (backed by an ArrayList<Person> ) and sort this list using a custom Comparator<Person> or by implementing Comparable<Person> in Person class. 我建议您创建一个名为Person的类,在其中存储名称字符串和双精度字符(无论其含义是什么),然后将Person每个实例存储在List<Person> (由ArrayList<Person> )中并对该列表进行排序使用自定义Comparator<Person>或通过在Person类中实现Comparable<Person>

You can use something like this (I always use a charset for reading, if you don't need it just don't use it): 您可以使用类似这样的东西(我总是使用字符集进行阅读,如果您不需要它,那就不要使用它):

List<Double> result = new LinkedList<>();

try (Scanner scanner = new Scanner(Paths.get("Grades.txt"), StandardCharsets.UTF_8.name())) {
  while (scanner.hasNextLine()) {
    result.add(Double.valueOf(scanner.nextLine().split(",")[1]));
  }
} catch (IOException e) {
  System.err.printf("Something happened here...this is why: %s", e);
}
Collections.sort(result, Collections.reverseOrder());
// Do your other stuff from now on...

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

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