0

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

用逗号:

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 没有将我的字符串分成正确数量的字符串。

4

5 回答 5

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++){

反而。

但是要回答您的问题,是的,您可以按照一些评论的建议拆分分号,尝试一下,看看它是否有效。

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

Edit2:这一行完全是多余的,应该删除,它只会阻塞代码并造成混淆。

RowData = new String[ITEMVARNUM];

Edit3:好的,我看到的基本问题是您使用一些未知变量作为界限。如果您正确跟踪 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);

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

于 2013-02-19T09:47:45.030 回答
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
于 2013-02-19T09:49:56.553 回答
0

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

于 2013-02-20T03:56:58.013 回答
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];
} 
于 2013-02-19T10:06:39.363 回答
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 的博客

我还在另一个问题中发现了这一点,这可能会帮助你 一个类似的问题

于 2013-02-19T09:46:40.717 回答