将键和值从映射写入文件

时间:2012-08-06 17:49:20

标签: java iteration

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

但它显示以下错误。

请有人帮助我。

   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();

            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你的第二个循环中的

Map.Entry entry = (Map.Entry) entryIter.next();正在崩溃。

在第一个遍历男性的while循环之后,你的迭代器已经到达集合的末尾,当你试图调用next()时它会中断。

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

将行更改为使用FentryIter

进行迭代
while (FentryIter.hasNext()) {
   Map.Entry entry = (Map.Entry) FentryIter.next();

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