【问题标题】:JAVA: ArrayIndexOutOFBoundException while reading imported txtJAVA:读取导入的 txt 时出现 ArrayIndexOutOFBoundException
【发布时间】:2014-10-24 12:24:08
【问题描述】:

我是初学者,我有一个 txt 文件,用户将导入到 java 中,我将读取 txt 文件 line be line 然后在每一行设置变量 base 并将它们添加到当前记录中

    public void importTXT() {
    JFileChooser fc = new JFileChooser();
    fc.setAcceptAllFileFilterUsed(false);
    fc.setMultiSelectionEnabled(false);
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "TEXT FILES", "txt", "text");
    fc.setFileFilter(filter);
    int returnVal = fc.showOpenDialog(CollectionFrame.this);
    String[] numstrs = null;
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File importedFile = fc.getSelectedFile();

        try {
            Scanner sc = new Scanner(importedFile);
            while (sc.hasNextLine()) {
                numstrs = sc.nextLine().split("\\s+"); // split by white
                                                        // space
            }
        } catch (IOException e) {

        }
        // add new collection
        Collection newCollection = new Collection(numstrs[0]);
        allRecord.addCollection(newCollection);

        // add art consignment information
        String consignmentName = numstrs[3];
        String description = numstrs[4];

我在倒数第二行收到了ArrayIndexOutOfBoundsException

String consignmentName = numstrs[3];

文本文件的内容是这样的:

Richman’s Estate Collection
5
ART
Water Lilies
A superb piece in great condition

谁能告诉我怎么了?

【问题讨论】:

  • 表示数组 numstrs 的元素少于 4 个。尝试使用 numstrs.length
  • Collection newCollection = new Collection(numstrs[0]); 应该做什么?你有自己的类型叫Collection吗?因为java.util中的Collection是一个接口
  • 先检查numstrs.length>4
  • 你应该使用调试器
  • 你知道把它放在循环之外你只使用最后一行并尝试使用它。当你用空格分割时检查最后一行不会有4个元素。

标签: java arrays exception


【解决方案1】:

编辑: 您当前正在阅读所有行并每次替换 numstrs
的值 因此,当您离开循环时,您只会得到其中最后一行的值。
我认为您想保存所有行 - 见下文。
结束编辑

你应该使用数组列表。

像这样:

ArrayList<String[]> numstrsList = new ArrayList<String[]>();
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File importedFile = fc.getSelectedFile();

        try {
            Scanner sc = new Scanner(importedFile);
            while (sc.hasNextLine()) {
                numstrsList.add(sc.nextLine().split("\\s+")); // split by white
                                                        // space
            }
        } catch (IOException e) {

        }
}

您可以通过以下方式获取数组列表的值:

for(int i=0:i<numstrsList.size();i++){
    String[] oneLineStrArray = numstrsList.get(index)
    //do something 
}

您应该发布文本数据,否则我们无法帮助您解决 OutOfBounds 错误。 另外,我想知道如何实例化一个集合。

【讨论】:

    【解决方案2】:

    正如之前的 cmets 已经建议的那样,该文件显然不包含您期望的形式的信息。 我建议您在扫描仪读取后为其添加 som 错误处理。

    if (numstrs.length < 5){
      ///TODO: add some handling here, exception or error dialog
    }
    

    【讨论】:

      【解决方案3】:

      这里先过ArrayIndexOutOfBoundsException

      在尝试访问索引值之前检查numstrs 长度。你可以这样做

      String consignmentName = null, description = null;
      if (numstrs.length >= 4) {
          consignmentName = numstrs[3];
      }
      if (numstrs.length >= 5) {
          description = numstrs[4];
      }
      

      此外,正如其中一条评论所指出的 - numstrs 将始终具有最后一行返回的值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-11
        • 2019-04-24
        • 2015-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-02
        相关资源
        最近更新 更多