【问题标题】:ArrayList from txt file trouble来自txt文件的ArrayList麻烦
【发布时间】:2012-01-31 14:53:13
【问题描述】:

4 类:

  1. Party - 一些生物的索引、名称、位置和列表(通过指向 Creature 类实例的链接访问)。
  2. Creature - 索引、类型、名称、索引派对、移情值、恐惧值、宝藏清单、文物清单
  3. Treasure - 索引、类型、生物按索引、权重、值
  4. Artifact - 索引、类型、索引生物、其他字段

使用 ArrayList 类来保存上述类的实例。

测试数据(看起来像):

p : 10003 : Conglomeration

c : 20001 : Woman   : Lucy   :10001 : 17 : 22 : 20

t : 30004 : Silver : 20005 : 120 : 1000

a : 40001 : Wand : 20007 : ElderWand

这是我到目前为止所写的:

import java.io.IOException;
import java.util.*;
import javax.swing.JFileChooser;
import java.util.Scanner;

public class SorcerersCave {

    public static void main(String[] args) {

        ArrayList<Party> partyList = new ArrayList<Party>();
        ArrayList<Creature> creatureList = new ArrayList<Creature>();
        ArrayList<Treasure> treasureList = new ArrayList<Treasure>();
        ArrayList<Artifact> artifactList = new ArrayList<Artifact>();


            // open and read file:
            try {
                Scanner scanner = new Scanner(chooser.getSelectedFile())
                        .useDelimiter("\\s*:\\s*");

                while (scanner.hasNextLine()) {

                    String line = scanner.nextLine();

                    int index;
                    String type;
                    String name;

                    char identifier = line.charAt(0);

                    if (identifier == 'p') {
                        index = scanner.nextInt();
                        name = scanner.next();
                        partyList.add(new Party(partyInfo(index, name)));

                    } else if (identifier == 'c') {
                        index = scanner.nextInt();
                        type = scanner.next();
                        name = scanner.next();
                        int partyC = scanner.nextInt();
                        int empathyC = scanner.nextInt();
                        double carryingCapacityC = scanner.nextDouble();
                        creatureList.add(new Creature(creatureInfo(index, type,
                                name, partyC, empathyC, carryingCapacityC)));

                    } else if (identifier == 't') {
                        index = scanner.nextInt();
                        type = scanner.next();
                        int creatureT = scanner.nextInt();
                        double weightT = scanner.nextDouble();
                        int valueT = scanner.nextInt();
                        treasureList.add(new Treasure(treasureInfo(index, type,
                                creatureT, weightT, valueT)));

                    } else if (identifier == 'a') {
                        index = scanner.nextInt();
                        type = scanner.next();
                        int creatureA = scanner.nextInt();
                        artifactList.add(new Artifact(artifactInfo(index, type,
                                creatureA)));

                    } else {
                        System.out.println("This is not a valid line of input");
                    }
                    System.out.println("Identifier: " + identifier);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("party: " + partyList.toString());
        }
    }

    private class Creature {

    public Creature (int index, String type, String name,
            int partyC, int empathyC, double carryingCapacityC) {
        return;
    }
}
    private class Party {

        public Party(int index, String name) {
            return;
        }
    }

    private class Artifact {

        public Artifact(int index, String type, int creatureA) {
            return;
        }
}
        private class Treasure {
            public Treasure(int index, String type, int creatureT,
                    double weightT, int valueT) {
                return ;
            }

}

我知道我已经遇到了问题,因为当我尝试打印 partyList 数组的内容时。它是空的。我就是不明白为什么。

存在的错误:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at SorcerersCave.main(SorcerersCave.java:43)

想法** 我是否有可能拥有 if-else 循环和“查看”相应的类来查看要扫描的内容(而不是在循环中完成所有操作)?例如:如果第一个字母是“p”,就去派对班看看如何处理这条线。

【问题讨论】:

  • 请删除所有的 GUI 内容,只发布与解析您的输入相关的代码。
  • 到目前为止您用于诊断问题的步骤是什么?
  • 我还建议您将静态数据放在 XML 文件中,因为它开始变得有点冗长和详细。
  • 我删除了一堆...更好? (对此感到抱歉)
  • 我很好奇为什么你要扮演人类调试器的角色,每次有人发现问题时问题都会改变。我知道我们应该互相帮助。但从我的角度来看,OP 不知道如何解决基本问题。他只是复制粘贴一些骨架实现,现在你们都帮他解决它。我可能会担心***中的痛苦。但这违反了我认为的 SO 政策。如有错误请指正。

标签: java arraylist


【解决方案1】:

我认为您的问题是您扫描了这些行中的所有输入:

           while (scanner.hasNext())
                    System.out.println(scanner.nextLine());

而且你的课看起来很不寻常。为什么使用static Object
只需为每种类型构建常规类,例如:

private class Creature{
   // data fields

   public Creature(int index, String type, String name, int partyC, int empathyC, double carryingCapacityC){
      // set data field values
   }

   // accessors, mutators etc.
}

类似这样的: http://goo.gl/hM2Fo

【讨论】:

  • 其实……可能就是这样。让我尝试改变它!
  • 好的!现在我想出了一些错误(我会在我的问题中发布它们)
  • System.out.println 是你的朋友。使用它来查看代码中的阶段。例如,打印String line = scanner.nextLine(); 的值只是为了查看代码是否到达它。
  • 它不会打印任何东西,因为我认为它卡在“InputMismatchException”上......这意味着我告诉它寻找它找不到的东西对吗? (即寻找一个 int 但接下来的事情不是一个 int?)
  • 这是您的问题: 1) 您必须创建一个新的扫描仪来扫描文本行。一旦您扫描了nextLine(),扫描仪就会移动到文件的下一行。 2) 您必须调用lineScan.next() 才能扫描标识符字符。 3) 我不确定您的分隔符模式是否正确 - 使用我建议的用法并确保您的输入数据格式正确。 4)你应该练习调试。并阅读有关 Scanners 和 Classes 的内容 - 研究 API。 :)
【解决方案2】:

我的建议是,你应该尝试使用调试模式。

PS。主要问题是你试图从无到有创建一个对象。

【讨论】:

    【解决方案3】:

    如果我没看错,你所有的对象都返回 null。然后,您尝试将空引用附加到 arrayList 并且它忽略它。尝试实际制作对象并查看结果。

    来自关于 add() 的 Collections javadoc

    Collections that support this operation may place limitations on what elements may be 
    added to this collection. In particular, some collections will refuse to add null
    elements, and others will impose restrictions on the type of elements that may be added.
    Collection classes should clearly specify in their documentation any restrictions on what  
    elements may be added.
    

    【讨论】:

      猜你喜欢
      • 2011-12-11
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      • 2023-04-07
      • 1970-01-01
      • 2017-08-16
      相关资源
      最近更新 更多