Java - String.split不会以分号分隔

时间:2013-02-19 09:35:49

标签: java android regex string

我的代码在用逗号分隔之前完美运行,现在它不适用于分号!

逗号:

public void loadXXXData() {
    InputStream is = getResources().openRawResource(R.raw.xxxdata);

    try
    {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        try { 
            String line; 
            String[] RowData;
            RowData = new String[XXXVARNUM];
            for(int i=0;i<NUMBEROFXXX;i++){
                //Read the line.
                line = reader.readLine();
                //Cut the line up.
                RowData = line.split(",",XXXVARNUM);
                for(int j=0;j<XXXVARNUM;j++){
                    //Distribute the line into a 2D array.
                    XXX[i][j] = RowData[j];
                }
            }
            //Populate forms
            setCurrentXXX(XXX);
        } 
        catch (IOException ex) { 
            Log.v("XXXdex", "I/O Exception: Could not read from the I/O stream.");
        }
        finally { 

            try { 
                //Close the stream
                is.close(); 
            } 
            catch (IOException e) {
                Log.v("XXXdex", "I/O Exception: Could not close the I/O stream.");
            }
            finally {

            }
        } 

    }
    finally {

    }
}

以分号:

public void loadItemData() {
    InputStream is = getResources().openRawResource(R.raw.items);

    try
    {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        try { 
            String line; 
            String[] RowData;
            RowData = new String[ITEMVARNUM];
            for(int i=0;i<NUMBEROFITEMS;i++){
                //Read the line.
                line = reader.readLine();
                //Cut the line up.
                RowData = line.split(";",ITEMVARNUM);
                for(int j=0;j<ITEMVARNUM;j++){
                    //Distribute the line into a 2D array.
                    ITEM[i][j] = RowData[j];
                }
            }
            //Populate forms
            setCurrentItem(item);
        } 
        catch (IOException ex) { 
            Log.v("XXXdex", "I/O Exception: Could not read from the I/O stream.");
        }
        finally { 

            try { 
                //Close the stream
                is.close(); 
            } 
            catch (IOException e) {
                Log.v("XXXdex", "I/O Exception: Could not close the I/O stream.");
            }
            finally {

            }
        } 

    }
    finally {

    }
}

来自xxxData的示例:

 1,dinosaur,Grass,Poison,45,49,49,65,65,45,Supergrow,,DrPhyll,64,0,0,0,1,0,0,45,0.7 m,6.9 kg,Seed

......装得很完。

来自itemData的示例:

 Antidote;A spray-type medicine. It lifts the effect of poison from one XXX.;Route 3, Pinwheel Forest, Pinwheel Forest, Pinwheel Forest (With Dowsing Machine), Icirrus City Shop Route 9, Accumula Town, Striaton City, Nacrene City, Castelia City, Nimbasa City, Driftveil City, Mistralton City, Icirrus City, Opelucid City, XXX League, Lacunosa Town, Undella Town, Black City, White Forest

...不会正确拆分。怎么了?我搜索论坛寻找代表“;”的正则表达式,但没有任何方法可行。我一直得到一个arrayIndexOutOfBounds异常,因为String.split没有将我的字符串分成正确的字符串数。

5 个答案:

答案 0 :(得分:1)

您的问题可能是您使用常量作为上限循环数组:

RowData = line.split(";",ITEMVARNUM);
            for(int j=0;j<ITEMVARNUM;j++){
                //Distribute the line into a 2D array.
                ITEM[i][j] = RowData[j];
            }

尝试使用

for(int j=0;j<RowData.length;j++){

代替。

但是要回答你的问题,是的,你可以拆分分号,正如一些评论所暗示的那样,尝试一下,看看它是否有效。

编辑:而且,我认为你误解了分裂方法的第二个参数。它只是一个上限,它不会使一个数组具有您指定的空位数,它只会将结果数组限制为不大于该值。

EDIT2: 这条线是完全冗余的,应该被删除,它只会堵塞代码并造成混淆。

RowData = new String[ITEMVARNUM];

EDIT3: 好吧,我看到的基本问题是你使用一些未知变量作为界限。如果您正确地跟踪indata并相应地更改这些变量,它可能会起作用。但是更好的方法是在实际的indata上进行操作。 没试过这个代码,但它应该工作(可能有一些小调整)

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        try { 
            String line=""; 
            String[] RowData;
            List<String[]> itemList = new ArrayList<String[]>();

            while(line!=null){
                //Read the line.
                line = reader.readLine();
                //Cut the line up.
                RowData = line.split(";");
                itemList.add(RowData);
            }
            String[][] item = new String[itemList.size()][];
            for(int i=0; i<itemList.size(); i++){
                item[i] = itemList.get(i);
            }
            //Populate forms
            setCurrentItem(item);

给它一个漩涡,看它是否有效。

答案 1 :(得分:1)

也许有些行包含的列少于预期。尝试捕获异常,并记录额外的数据,例如行的内容,您拥有的部分等。

您也可以尝试调试,在Eclipse中设置异常断点。


您的测试数据有问题。试试这个:

Antidote;A spray-type medicine. It lifts the effect of poison from one XXX.;Route 3; Pinwheel Forest; Pinwheel Forest; Pinwheel Forest (With Dowsing Machine); Icirrus City Shop Route 9; Accumula Town; Striaton City; Nacrene City; Castelia City; Nimbasa City; Driftveil City; Mistralton City; Icirrus City; Opelucid City; XXX League; Lacunosa Town; Undella Town; Black City; White Forest

答案 2 :(得分:0)

尝试这种简单的分割方法,在这里我用逗号和空格分割。

temp=”10054, 10055, 10056, 10035, 10052, 10036, 10037, 10038″;
String[] url = temp.split(",\\s+");

这是你得到的结果 url = [10054,10055,10056,10035,10052,10036,10037,10038]

希望你明白

有关详细说明,请参阅此博客Amalan's Blog

我也在另一个问题中发现这可能会对你有帮助 A similar problem

答案 3 :(得分:0)

public static void main(String[] args) {
    String str = "Antidote;A spray-type medicine. It lifts the effect of poison from one XXX.;Route 3, Pinwheel Forest, Pinwheel Forest, Pinwheel Forest (With Dowsing Machine), Icirrus City Shop Route 9, Accumula Town, Striaton City, Nacrene City, Castelia City, Nimbasa City, Driftveil City, Mistralton City, Icirrus City, Opelucid City, XXX League, Lacunosa Town, Undella Town, Black City, White Forest";
    String[] s = str.split(";");
    System.out.println(s.length);
    for(String string: s)
        System.out.println(string);
}

由于您已经提供了示例输入,因此字符串将被拆分为3个部分,RowData的大小将为3.因此,如果ITEMVARNUM大于3,您将获得arrayIndexOutOfBounds异常。

RowData = line.split(";",ITEMVARNUM);
for(int j=0;j<ITEMVARNUM;j++){
  //Distribute the line into a 2D array.
   ITEM[i][j] = RowData[j];
} 

答案 4 :(得分:0)

想出来......当我从网站上复制并粘贴数据时,在“Shop”之前和之后,以某种方式存储在.txt文件中的行尾字符。我不明白我怎么在记事本中看不到它,但我用分号替换了每个单词“Shop”以制作四个单独的字段,并且瞧。没有更多的混乱。谢谢你们的想法。