开发者

Android parse String ArrayIndexOutOfBoundsException

开发者 https://www.devze.com 2023-04-04 07:53 出处:网络
I\'m trying to parse a String to obtain 3 Integer but I have a Force Close and the LogCat says : ArrayIndexOutOfBoundExceptions.

I'm trying to parse a String to obtain 3 Integer but I have a Force Close and the LogCat says : ArrayIndexOutOfBoundExceptions.

Here is the concerned part of my code :

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

    String[] separatedDate = dateModi开发者_StackOverflowf.split(".");

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

I checked the value of the string with a toast and it contains values like, for example : 13.9.2011

The mistake comes from this line :

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

(If I put it as comment, it gives the same mistake frome the next line)

Thanks for your help!


String.split() takes a regex in which case . means "any character". You'll want to escape it like this: \.. And since you specify the regex as a String literal, you'll need to double the backslashes: dateModif.split("\\.").

But it would be better to use real date parsing methods for this.


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

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

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

check this out

0

精彩评论

暂无评论...
验证码 换一张
取 消