I have written this code to display the stable marriages between the given group of people and their preferences. I believe my code is complete however I received the error
"Exception in thread "main" java.lang.Error: Unresolved compilation problem: at StableMarriage.main(StableMarriage.java:26)"
The error is listed on line 20 where the main method begins, I thought this error was due to a problem reading the arrays but I am not sure how to fix it.
public class StableMarriage {
static List<String> men = Arrays.asList(
new String[] {"John", "Robert", "Brian", "Stephen", "George"});
static List<String> women = Arrays.asList(
new String[]{"Nancy", "Joyce", "Patricia", "Anne", "Susan"});
static Map<String, List<String>> menChoose = new HashMap<String, List<String>>(){{
put("John", Arrays.asList("Susan", "Joyce", "Nancy", "Patricia", "Anne"));
put("Robert", Arrays.asList("Nancy", "Anne", "Joyce", "Susan", "Patricia"));
put("Brian", Arrays.asList("Patricia", "Susan", "Joyce", "Anne", "Nancy"));
put("Stephen", Arrays.asList("Joyce", "Anne", "Susan", "Nancy", "Patricia"));
put("George", Arrays.asList("Nancy", "Joyce", "Patricia", "Susan", "Anne"));
}};
static Map<String, List<String>> womenChoose = new HashMap<String, List<String>>(){{
put("Nancy", Arrays.asList("John", "Brian", "Stephen", "Robert", "George"));
put("Joyce", Arrays.asList("George", "John", "Stephen", "Robert", "Brian"));
put("Patricia", Arrays.asList("George", "Brian", "Robert", "Stephen", "John"));
put("Anne", Arrays.asList("George", "Stephen", "John", "Brian", "Robert"));
put("Susan", Arrays.asList("Brian", "George", "Stephen", "John", "Robert"));
}};
public static void main(String[] args) throws Exception {
File ();
Map<String, String> matches = match(men, menChoose, womenChoose);
for(Map.Entry<String, String> couple:matches.entrySet())
System.out.println(couple.getKey() + " engaged to " + couple.getValue());
if(checkMatches(men, women, matches, menChoose, womenChoose)) {
System.out.println("Stable Matches");
} else {
System.out.println("Unstable");
}
String tmp = matches.get(women.get(0));
matches.put(women.get(0), matches.get(women.get(1)));
matches.put(women.get(1), tmp);
System.out.println(women.get(0) + " and " + women.get(1) + " have switched partners");
if(checkMatches(men, women, matches, menChoose, womenChoose)) {
System.out.println("Stable");
} else {
System.out.println("Unstable");
}
}
private static Map<String, String> match(List<String> men2,
Map<String, List<String>> menChoose2,
Map<String, List<String>> womenChoose2) {
return null;
}
public static Map<String, String> match(List<String> men, Map<String, List<String>> menChoose, Map<String>> womenChoose) {
Map<String, String> engagedTo = new TreeMap<String, String>();
List<String> freeMen = new LinkedList<String>();
freeMen.addAll(men);
while(!freeMen.isEmpty()){
String currentMan = freeMen.remove(0);
List<String> manPrefers = menChoose.get(currentMan);
for(String woman:manPrefers){
if(engagedTo.get(woman) == null) {
engagedTo.put(woman, currentMan);
break;
} else {
String otherMan = engagedTo.get(woman);
List<String> womanPrefers = womenChoose.get(woman);
if(womanPrefers.indexOf(currentMan) < womanPrefers.indexOf(otherMan)) {
engagedTo.put(woman, currentMan);
freeMen.add(otherMan);
break;
}
}
}
}
return engagedTo;
}
private static boolean checkMatches(List<String> men, List<String> women, Map<String, String> matches,
Map<String, List<String>> menChoose, Map<String, List<String>> womenChoose) {
if(!matches.keySet().containsAll(women))
return false;
if(!matches.values().containsAll(men))
return false;
Map<String, String> invertedMatches = new TreeMap<String, String>();
for(Map.Entry<String, String> couple:matches.entrySet())
invertedMatches.put(couple.getValue(), couple.getKey());
for(Map.Entry<String, String> couple:matches.entrySet()) {
List<String> shePrefers = womenChoose.get(couple.getKey());
List<String> sheLikes = new LinkedList<String>();
sheLikes.addAll(shePrefers.subList(0, shePrefers.indexOf(couple.getKey())));
List<String> hePrefers = menChoose.get(couple.getValue());
List<String> heLikes = new LinkedList<String>();
heLikes.addAll(hePrefers.subList(0, hePrefers.indexOf(couple.getKey())));
for(String man:sheLikes) {
String manWife = invertedMatches.get(man);
List<String> manPrefers = menChoose.get(man);
if(manPrefers.indexOf(manWife) > manPrefers.indexOf(couple.getKey())) {
System.out.printf("%s likes %s better than %s and %s likes %s better than their current partner \n",
couple.getKey(), man, couple.getValue(), man, couple.getKey());
return false;
}
}
for(String woman:heLikes) {
String sheHusband = matches.get(woman);
List<String> womanPrefers = womenChoose.get(woman);
if(womanPrefers.indexOf(sheHusband) > womanPrefers.indexOf(couple.getValue())) {
System.out.printf("%s likes %s better than %s and %s like %s better than their current partner \n",
couple.getValue(), woman, couple.getKey(), woman, couple.getValue());
return false;
}
}
}
return true;
}