【问题标题】:Splitting error- IndexOutOfBoundsException拆分错误 - IndexOutOfBoundsException
【发布时间】:2014-07-03 15:43:23
【问题描述】:

我遇到了一个似乎无法解决的问题。拆分时,我应该能够通过设置 row[0]、row[1]、row[2] 来获取 id、name、check。奇怪的是,只有 row[0] (id) 似乎有效。 名字,检查给我一个错误。有人可以进一步帮助我吗?

数据示例:

id,name,check
1,john,0
1,patrick,0
1,naruto,0

代码:

    ArrayList<String> names = new ArrayList<String>();
    try {

        DataInputStream dis = new DataInputStream(openFileInput(listLocation(listLoc)));
        BufferedReader br = new BufferedReader(new InputStreamReader(dis));
        String line;
        while ((line = br.readLine()) != null) {
             String[] row = line.split(Pattern.quote(","));
                 //names.add(row[0]); // id
                 names.add(row[1]); // name // ERROR AT THIS LINE
                 //names.add(row[2]); // check
        }
        br.close();
    }
    catch (IOException e) {
        e.printStackTrace();
    }

错误信息:

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

已解决 文件末尾的值(问号)似乎不正确。删除此行时。我的代码有效(没有 Patter.quote)。谢谢大家的快速回复。第一个答案帮助我提醒我在可以看到“不正确值”的地方使用 Log 值。我的错。

【问题讨论】:

  • Pattern.quote(",") 返回什么?你输入的字符串是什么
  • 唯一可能的错误似乎是,读取 line 中没有','。处理前打印并验证其数据是否正确
  • 简单使用:String[] row = line.split(","); 因为Pattern.quote 不会拆分任何内容,您将获得完整的未拆分行。 (这将导致 length = 1,而不是预期的 lenght = 3
  • 逗号在正则表达式中没有任何特殊含义,因此不需要传递任何Pattern.quote()。你确定你正在阅读的字符串的内容吗?逗号真的是分隔值吗?如果打印出读取的行,是否正确?
  • String regularExp="(\\d+)(,)([^,]*)(,)(\\d+)";模式 p = Pattern.compile(regualrExp,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);匹配器 m = p.matcher(txt); if (m.find()) { String id =m.group(1);字符串逗号1=m.group(2);字符串名称=m.group(3);字符串逗号2=m.group(4);字符串检查 = m.group(5); }

标签: java android split


【解决方案1】:

大概在那个时候:

String[] row = line.split(",");

被调用,您尝试读取的文件/流的行中没有逗号 (,)。

【讨论】:

  • 是的,文件末尾可能有空行
  • Cyto,我认为您正在寻找错误的方向。试着先看一下 br.readLine() ,看看是不是每次调用它都会返回一个包含逗号的字符串。
  • 有趣。我记录了 br.readline()。它在我添加时起作用: Log.d("name", br.readLine());在循环结束时。 oO
  • 我发布了一种更好的方法。见下文。
【解决方案2】:

试试这个代码,

ArrayList<String> names = new ArrayList<String>();
    try {

        DataInputStream dis = new DataInputStream(openFileInput(listLocation(listLoc)));
        BufferedReader br = new BufferedReader(new InputStreamReader(dis));
        String line;
        while ((line = br.readLine()) != null) {
            String[] row = line.split(",");
            //names.add(row[0]); // id
            names.add(row[1]); // name // ERROR AT THIS LINE
            //names.add(row[2]); // check
        }
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

我认为你不需要在这里使用Pattern.quote(",")

【讨论】:

    【解决方案3】:

    根据我对 txt 文件的经验,这是处理它的最佳方式:

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.regex.Pattern;
    
    public class Cyto {
    
        public static void main(String[] args) throws IOException {
            ArrayList<String> names = new ArrayList<String>();
            try {
    
                FileInputStream dis = new FileInputStream("list.txt");
                BufferedReader br = new BufferedReader(new InputStreamReader(dis));
                String line;
                while (br.ready()) {
                    line = br.readLine();
                    String[] row = line.split(Pattern.quote(","));      
                    System.out.println(row[1]);
                    names.add(row[1]);
                }
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }
    

    使用

    br.ready()
    

    而不是直接从流中读取。

    【讨论】:

    • 谢谢,我正在使用 br.ready()。 @FileInputStream 我得到一个错误打开失败:没有这样的文件或目录我的方法(DataInputStream)工作不知道它是否更好。
    • 我将您的代码改编为我的,因为我不知道您的目录/文件结构如何。
    猜你喜欢
    • 2015-11-07
    • 1970-01-01
    • 2012-05-03
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 2018-09-07
    相关资源
    最近更新 更多