在 Java 项目中读取.txt文件

Reading a .txt file in Java project

提问人:Shawn 提问时间:10/5/2023 更新时间:10/6/2023 访问量:56

问:

我正在开发一个 Java 程序,该程序可以逐行读取输入1.txt文本文件,每个文件都有一个数字,例如: 2 1 2 2 1 2 1 1 2

该程序假设取每个数字并抛出以实现 Gale-Shapley 稳定匹配。 我需要帮助让程序读取文本文件。

我收到以下错误消息: 进程完成,退出代码为 0

我在我的“src”和主项目文件夹中有一个输入1.txt文件的副本,但它仍然找不到该文件。我尝试使用 VS Code 和 IntelliJ IDEA,但收到了相同的错误消息。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class Project1 {

    public static void main(String[] args) {
        if (args.length != 1) {
            return;
        }

        try {
            String inputFile = "input1.txt"; // Specify the correct file path if necessary
            List<List<Integer>> menPrefList = new ArrayList<>();
            List<List<Integer>> womenPrefList = new ArrayList<>();

            BufferedReader br = new BufferedReader(new FileReader(inputFile));
            int n = Integer.parseInt(br.readLine());

            for (int i = 0; i < n; i++) {
                menPrefList.add(parsePreferenceList(br.readLine()));
            }

            for (int i = 0; i < n; i++) {
                womenPrefList.add(parsePreferenceList(br.readLine()));
            }

            Map<Integer, Integer> menToWomen = menPropose(menPrefList, womenPrefList);
            Map<Integer, Integer> womenToMen = womenPropose(menPrefList, womenPrefList);

            System.out.println("Output when men propose:");
            printMatching(menToWomen);

            System.out.println("Output when women propose:");
            printMatching(womenToMen);

            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<Integer> parsePreferenceList(String line) {
        String[] tokens = line.split(" ");
        List<Integer> preferenceList = new ArrayList<>();
        for (String token : tokens) {
            preferenceList.add(Integer.parseInt(token));
        }
        return preferenceList;
    }

    public static Map<Integer, Integer> menPropose(List<List<Integer>> menPrefList, List<List<Integer>> womenPrefList) {
        int n = menPrefList.size();
        Map<Integer, Integer> womenMatches = new HashMap<>();
        boolean[] menEngaged = new boolean[n];

        Queue<Integer> freeMen = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            freeMen.offer(i);
        }

        while (!freeMen.isEmpty()) {
            int man = freeMen.poll();
            List<Integer> preferences = menPrefList.get(man);

            for (int woman : preferences) {
                if (!menEngaged[man]) {
                    if (!womenMatches.containsKey(woman)) {
                        womenMatches.put(woman, man);
                        menEngaged[man] = true;
                        break;
                    } else {
                        int currentMan = womenMatches.get(woman);
                        List<Integer> womanPreferences = womenPrefList.get(woman);
                        if (womanPreferences.indexOf(man) < womanPreferences.indexOf(currentMan)) {
                            womenMatches.put(woman, man);
                            menEngaged[man] = true;
                            freeMen.offer(currentMan);
                            break;
                        }
                    }
                }
            }
        }
        return womenMatches;
    }

    public static Map<Integer, Integer> womenPropose(List<List<Integer>> menPrefList, List<List<Integer>> womenPrefList) {
        int n = womenPrefList.size();
        Map<Integer, Integer> menMatches = new HashMap<>();
        boolean[] womenEngaged = new boolean[n];

        Queue<Integer> freeWomen = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            freeWomen.offer(i);
        }

        while (!freeWomen.isEmpty()) {
            int woman = freeWomen.poll();
            List<Integer> preferences = womenPrefList.get(woman);

            for (int man : preferences) {
                if (!womenEngaged[woman]) {
                    if (!menMatches.containsKey(man)) {
                        menMatches.put(man, woman);
                        womenEngaged[woman] = true;
                        break;
                    } else {
                        int currentWoman = menMatches.get(man);
                        List<Integer> manPreferences = menPrefList.get(man);
                        if (manPreferences.indexOf(woman) < manPreferences.indexOf(currentWoman)) {
                            menMatches.put(man, woman);
                            womenEngaged[woman] = true;
                            freeWomen.offer(currentWoman);
                            break;
                        }
                    }
                }
            }
        }
        return menMatches;
    }

    public static void printMatching(Map<Integer, Integer> matching) {
        System.out.print("{");
        boolean first = true;
        for (Map.Entry<Integer, Integer> entry : matching.entrySet()) {
            if (!first) {
                System.out.print(", ");
            }
            System.out.print("(" + (entry.getKey() + 1) + "," + (entry.getValue() + 1) + ")");
            first = false;
        }
        System.out.println("}");
    }
}


如果input1.txt包含: 2 1 2 2 1 2 1 1 2 然后输出应该是: 男性求婚时的输出: {(1,1), (2,2)} → 表示 {(m1, w1), (m2, w2)} 女性求婚时的产出: {(1,2), (2,1)} → 表示 ((w1, m2), (w2, m1)}

java 文件-io 文本文件 java.util.scanner

评论

1赞 Tim Moore 10/5/2023
除非只提供一个参数,否则此程序将立即退出。它似乎没有使用参数。我的猜测是您在运行它时没有提供一个。也许你不需要最上面的声明?if
0赞 hermit 10/5/2023
您是否在程序中提供参数?

答:

0赞 Ralf Ulrich 10/6/2023 #1

至少,删除以下行:

   if (args.length != 1) {
            return;
        }

因为您在代码中没有任何地方使用,如果您没有确切指定一个参数(我们从您的问题中不知道),这只会以退出代码 0 结束您的代码。args