简体   繁体   中英

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:

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. 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); but never use the buffer to read the data, instead you use the 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. You should pass it as a File instead, like this:

     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 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.

  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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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