0

使用下面的代码,我试图将男性和女性地图的键和值写入现有文件。

但它显示以下错误。

有人可以帮我吗。

   ERROR@      Map.Entry entry = (Map.Entry) entryIter.next();

   java.util.NoSuchElementException
   at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(Unknown Source)
   at java.util.LinkedHashMap$EntryIterator.next(Unknown Source)
   at java.util.LinkedHashMap$EntryIterator.next(Unknown Source)
   at test.main(test.java:83)

代码

public static void main(String[] args) {
    Map<String, List<String>> MaleMap = new LinkedHashMap<String, List<String>>();
    Map<String, List<String>> FemaleMap = new LinkedHashMap<String, List<String>>();
    try {
        Scanner scanner = new Scanner(new FileReader(".txt"));

        while (scanner.hasNextLine()) {
            String nextLine = scanner.nextLine();
            String[] column = nextLine.split(":");
            if (column[0].equals("Male") && (column.length == 4)) {
                MaleMap.put(column[1],
                        Arrays.asList(column[2], column[3]));
            } else if (column[0].equals("Female") && (column.length == 4)) {
                FemaleMap.put(column[1],
                        Arrays.asList(column[2], column[3]));
            }
        }
        Set<Entry<String, List<String>>> entries = MaleMap.entrySet();
        Iterator<Entry<String, List<String>>> entryIter = entries.iterator();
        while (entryIter.hasNext()) {
            Map.Entry entry = (Map.Entry) entryIter.next();
            Object key = entry.getKey(); // Get the key from the entry.

            List<String> value = (List<String>) entry.getValue();
            Object value1 = " ";
            Object value2 = " ";
            int counter = 0;
            for (Object listItem : (List) value) {
                Writer writer = null;
                Object maleName = key;
                Object maleAge = null;
                Object maleID = null;
                if (counter == 0) {// first pass assign value to value1
                    value1 = listItem;
                    counter++;// increment for next pass
                } else if (counter == 1) {// second pass assign value to value2
                    value2 = listItem;
                    counter++;// so we dont keep re-assigning listItem for further iterations
                }
            }
            System.out.println(key + ":" + value1 + "," + value2);
            scanner.close();
            Writer writer = null;
            Object maleName = key;
            Object maleAge = value1;
            Object maleID = value2;
            try {
                String filename = ".txt";
                FileWriter fw = new FileWriter(filename, true); // the true will append the new data
                fw.write(maleAge + "." + maleID + "@@;" + "\n"
                        + " :class :" + maleName);// appends the string to the file
                fw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        Set<Entry<String, List<String>>> Fentries = FemaleMap.entrySet();
        Iterator<Entry<String, List<String>>> FentryIter = Fentries.iterator();
        while (FentryIter.hasNext()) {
            Map.Entry entry = (Map.Entry) entryIter.next();
            Object Fkey = entry.getKey(); // Get the key from the entry.

            List<String> value = (List<String>) entry.getValue();
            Object value1 = " ";
            Object value2 = " ";
            int counter = 0;
            for (Object listItem : (List) value) {
                Writer writer = null;
                Object femaleName = Fkey;
                Object femaleAge = null;
                Object femaleID = null;
                if (counter == 0) {// first pass assign value to value1
                    value1 = listItem;
                    counter++;// increment for next pass
                } else if (counter == 1) {// second pass assign value to value2
                    value2 = listItem;
                    counter++;// so we dont keep re-assigning listItem for further iterations
                }
            }
            System.out.println(Fkey + ":" + value1 + "," + value2);
            scanner.close();
            Writer writer = null;
            Object femaleName = Fkey;
            Object femaleAge = value1;
            Object femaleID = value2;
            try {
                String filename = ".txt";
                FileWriter fw = new FileWriter(filename, true); // the true will append the new data
                fw.write("map:" + femaleName + " a :Bridge;" + "\n"
                        + ":property" + femaleAge + ";" + "\n"
                        + ":column" + " " + femaleID + " " + ";");// appends the string to the file
                fw.close();
            } catch (Exception e) {
                e.printStackTrace();

            }
        }
    }
}
4

1 回答 1

1

Map.Entry entry = (Map.Entry) entryIter.next();在你的第二个循环是打破。

在遍历男性的第一个 while 循环之后,您的迭代器已到达集合的末尾,当您尝试调用时将中断next()

你真正想做的是遍历你的女性。

更改行以使用您的迭代FentryIter

while (FentryIter.hasNext()) {
   Map.Entry entry = (Map.Entry) FentryIter.next();

这很可能是复制粘贴代码的结果,您在执行此操作时需要小心。我建议重构您的代码,因为其中有很多是重复的。

于 2012-08-06T17:52:40.983 回答