我正在开发一个 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)}
根据您的错误,input1.txt是一个文件,并且您直接使用该文件名,因此根据您的场景的路径匹配条件,您的java程序和这个文件(input1.txt)这两个文件应该具有相同的位置。例如,您的程序位于 D 驱动器中,如 D -> Java -> [Your-java-file] 那么您的文本文件应该位于相同位置,如 D -> Java -> [Your-text-file] 然后它将给出完成输出并从各自的位置找到指定的文件。
如果位置不匹配,那么不是问题,但您需要为当前 java 程序中使用的文件提供绝对路径。