如何对使用Formatter创建的表格进行排序?

huangapple go评论101阅读模式
英文:

How to sort a table created with Formatter?

问题

我创建了一个从文件中读取文本、计算每个单词出现次数并将所有内容放入使用Formatter创建的表格的应用程序。现在我想按单词的出现次数对表格进行排序。以下是迄今为止的代码:

  1. package com.example.antonpaarapplication;
  2. import java.io.IOException;
  3. import java.nio.file.Files;
  4. import java.nio.file.Paths;
  5. import java.util.*;
  6. public class Application {
  7. // 初始化应用程序的函数。
  8. public void run() {
  9. System.out.println("您想处理整个文件(1)还是仅处理特定行(2)?");
  10. choice();
  11. }
  12. // 选择是一次性处理整个文件还是仅处理选择的特定文件的函数。
  13. public void choice() {
  14. Scanner scanner = new Scanner(System.in);
  15. int number = scanner.nextInt();
  16. switch (number) {
  17. case 1:
  18. System.out.println("\n已输入数字1,正在处理整个文件。\n");
  19. processWholeFile();
  20. break;
  21. case 2:
  22. System.out.println("\n已输入数字2,请指定要处理的行。\n");
  23. processSpecificLines();
  24. break;
  25. default:
  26. System.out.println("无效,请重试。");
  27. choice();
  28. }
  29. }
  30. // 处理整个文件的函数。
  31. public void processWholeFile() {
  32. Map<String, Integer> stringMap = new HashMap<>();
  33. Formatter formatter = new Formatter();
  34. List<String> lines = readFile();
  35. String string = String.join(" ", lines);
  36. String[] arr = string.split(" ");
  37. for (String s : arr) {
  38. if (stringMap.containsKey(s)) {
  39. stringMap.put(s, stringMap.get(s) + 1);
  40. } else {
  41. stringMap.put(s, 1);
  42. }
  43. }
  44. formatter.format("%20s %20s\n\n", "单词", "出现次数");
  45. for (Map.Entry<String, Integer> mapEntry : stringMap.entrySet()) {
  46. if (mapEntry.getKey().substring(0, 1).equals("1")) {
  47. formatter.format("%20s %20s\n", mapEntry.getKey(), mapEntry.getValue());
  48. } else {
  49. formatter.format("%20s %15s\n", mapEntry.getKey(), mapEntry.getValue());
  50. }
  51. }
  52. System.out.println(formatter);
  53. }
  54. // 处理文件特定部分的函数。
  55. public void processSpecificLines() {
  56. }
  57. // 读取"data.txt"的内容。
  58. public List<String> readFile() {
  59. List<String> lines = new ArrayList<>();
  60. try {
  61. lines = Files.readAllLines(Paths.get(System.getProperty("user.dir"), "src", "main", "resources", "data.txt"));
  62. } catch (IOException e) {
  63. throw new RuntimeException(e);
  64. }
  65. return lines;
  66. }
  67. public static void main(String[] args) {
  68. Application app = new Application();
  69. app.run();
  70. }
  71. }

我尝试在Google上搜索这个问题,但似乎要么不可能实现,要么没有人想过这个问题,因为我找不到相关信息。

英文:

I made an application that reads text from a file, counts each word, and puts everything into a table created with Formatter. Now I would like to sort the table by the number Occurrences per word. Here is my code so far:

java

  1. package com.example.antonpaarapplication;
  2. import java.io.IOException;
  3. import java.nio.file.Files;
  4. import java.nio.file.Paths;
  5. import java.util.*;
  6. public class Application {
  7. //Function for Initializing the Application.
  8. public void run() {
  9. System.out.println(&quot;Would you like to process the entire file(1) or just specific lines(2)?&quot;);
  10. choice();
  11. }
  12. //Function for choosing whether to process the entire file at once or only select specific files to be processed.
  13. public void choice() {
  14. Scanner scanner = new Scanner(System.in);
  15. int number = scanner.nextInt();
  16. switch (number) {
  17. case 1:
  18. System.out.println(&quot;\nNumber 1 has been entered, processing whole file.\n&quot;);
  19. processWholeFile();
  20. break;
  21. case 2:
  22. System.out.println(&quot;\nNumber 2 has been entered, please specify lines to be processed. \n&quot;);
  23. processSpecificLines();
  24. break;
  25. default:
  26. System.out.println(&quot;Invalid, please try again.&quot;);
  27. choice();
  28. }
  29. }
  30. //Function for processing the entire File.
  31. public void processWholeFile() {
  32. Map&lt;String, Integer&gt; stringMap = new HashMap&lt;String, Integer&gt;();
  33. Formatter formatter = new Formatter();
  34. List&lt;String&gt; lines = readFile();
  35. String string = String.join(&quot; &quot;, lines);
  36. String[] arr = string.split(&quot; &quot;);
  37. for (String s : arr) {
  38. if (stringMap.containsKey(s)) {
  39. stringMap.put(s, stringMap.get(s) + 1);
  40. } else {
  41. stringMap.put(s, 1);
  42. }
  43. }
  44. formatter.format(&quot;%20s %20s\n\n&quot;, &quot;Word&quot;, &quot;Occurence&quot;);
  45. for (Map.Entry&lt;String, Integer&gt; mapEntry : stringMap.entrySet()) {
  46. if (mapEntry.getKey().substring(0, 1).equals(&quot;1&quot;)) {
  47. formatter.format(&quot;%20s %20s\n&quot;, mapEntry.getKey(), mapEntry.getValue());
  48. } else {
  49. formatter.format(&quot;%20s %15s\n&quot;, mapEntry.getKey(), mapEntry.getValue());
  50. }
  51. }
  52. System.out.println(formatter);
  53. }
  54. //Function for processing specific parts of the file.
  55. public void processSpecificLines() {
  56. }
  57. //Reads the contents of the &quot;data.txt&quot;.
  58. public List&lt;String&gt; readFile() {
  59. List&lt;String&gt; lines = new ArrayList&lt;&gt;();
  60. try {
  61. lines = Files.readAllLines(Paths.get(System.getProperty(&quot;user.dir&quot;), &quot;src&quot;, &quot;main&quot;, &quot;resources&quot;, &quot;data.txt&quot;));
  62. } catch (IOException e) {
  63. throw new RuntimeException(e);
  64. }
  65. return lines;
  66. }
  67. public static void main(String[] args) {
  68. Application app = new Application();
  69. app.run();
  70. }
  71. }

I tried googling the problem, but it seems like this either not possible or nobody has every thought about it, as I can't find anything about it.

答案1

得分: 1

问题不在于 Formatter 本身,而在于在格式化之前如何存储数据,使用了一个不维护任何顺序的 HashMap。您可以改用一个可排序的集合,例如:

  1. List<Map.Entry<String, Integer>> sortedList = new ArrayList<>(stringMap.entrySet());
  2. sortedList.sort(Map.Entry.<String, Integer>comparingByValue().reversed());
  3. formatter.format("%20s %20s\n\n", "Word", "Occurrence");
  4. for (Map.Entry<String, Integer> mapEntry : sortedList) {
  5. if (mapEntry.getKey().substring(0, 1).equals("1")) {
  6. formatter.format("%20s %20s\n", mapEntry.getKey(), mapEntry.getValue());
  7. } else {
  8. formatter.format("%20s %15s\n", mapEntry.getKey(), mapEntry.getValue());
  9. }
  10. }
英文:

The problem is not with the Formatter itself, but how you're storing the data before formatting it, using a HashMap which doesn't maintain any ordering. You could use a sortable collection instead, like:

  1. List&lt;Map.Entry&lt;String, Integer&gt;&gt; sortedList = new ArrayList&lt;&gt;(stringMap.entrySet());
  2. sortedList.sort(Map.Entry.&lt;String, Integer&gt;comparingByValue().reversed());
  3. formatter.format(&quot;%20s %20s\n\n&quot;, &quot;Word&quot;, &quot;Occurrence&quot;);
  4. for (Map.Entry&lt;String, Integer&gt; mapEntry : sortedList) {
  5. if (mapEntry.getKey().substring(0, 1).equals(&quot;1&quot;)) {
  6. formatter.format(&quot;%20s %20s\n&quot;, mapEntry.getKey(), mapEntry.getValue());
  7. } else {
  8. formatter.format(&quot;%20s %15s\n&quot;, mapEntry.getKey(), mapEntry.getValue());
  9. }
  10. }

huangapple
  • 本文由 发表于 2023年7月3日 23:17:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76606078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定