2011-09-13 62 views
1

我想解析一个字符串来获得3整数,但我有一个强制关闭和LogCat说:ArrayIndexOutOfBoundExceptionsAndroid解析字符串ArrayIndexOutOfBoundsException

这里是我的代码的相关部分:

dateModif = tvDateAffichee.getText().toString(); 

    String[] separatedDate = dateModif.split("."); 

    mDay = Integer.parseInt(separatedDate[0]); 
    mMonth = Integer.parseInt(separatedDate[1]); 
    mYear = Integer.parseInt(separatedDate[2]); 

我查了敬酒的字符串值,并将其包含的值等,例如:2011年9月13日

的错误来自该行:

mDay = Integer.parseInt(separatedDate[0]); 

(如果我把它作为评论,它给了同样的错误弗罗姆下一行)

感谢您的帮助!

回答

3

String.split()需要一个正则表达式,在这种情况下.表示“任何字符”。你会想要这样逃避它:\.。而且,由于您将正则表达式指定为String文字,因此您需要将反斜杠加倍:dateModif.split("\\.")

但是最好使用真正的日期分析方法。

+0

感谢您的快速答复!我知道使用真正的日期分析会更好,但它是一个非常特殊的事情。 – HerrM

1
String dateModif = tvDateAffichee.getText().toString(); 

String []separatedDate=dateModif.split("[.]"); 

    mDay = Integer.parseInt(separatedDate[0]); 
    mMonth = Integer.parseInt(separatedDate[1]); 
    mYear = Integer.parseInt(separatedDate[2]); 

检查了这一点

+0

它也可以工作。谢谢! – HerrM