分割错误-IndexOutOfBoundsException [英] Splitting error- IndexOutOfBoundsException

查看:82
本文介绍了分割错误-IndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入了一个似乎无法解决的问题.分割时,我应该可以通过设置 row [0],row [1],row [2] 来获取 id,名称,检查.奇怪的是,只有row [0](id)似乎起作用.名称,检查给我一个错误.有人可以进一步帮助我吗?

I got stuck with an issue, that I can't seem to solve. When splitting I should be able to get id, name, check by setting row[0], row[1], row[2]. Strangely only row[0] (id) seem to work. Name, check gives me an error. Could someone help me further?

数据示例:

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).谢谢大家的快速答复.第一个答案帮助我提醒我在可以看到错误值"的地方使用对数值.我不好.

Solved It seems I had an incorrect value (question marks) at end of file. When removing this line. My code worked (without Patter.quote). Thank you all for fast reply's. First answer helped me on reminding me of using Log value where I could see the 'incorrect value'. My bad.

推荐答案

可能在该时间段:

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

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

was called, there was no comma (,) in the line of the file/stream you're trying to read.

这篇关于分割错误-IndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆