简体   繁体   中英

Android: Array Index Out Of Bounds Error

I am trying to convert a string containing date to a char array and then extract the month and convert it.

I'm using API, the server provide date in the format: "2015-04-10", I'm trying to convert this into: "10 April, 2015."

  1. Im storing the date received from server is in a string.
  2. I'm converting the entire string into a char array.
  3. I'm running a loop through this array and separating it into three arrays: YEAR, MONTH, DAY.
  4. I'm separately converting these arrays to strings.
  5. Then I'm comparing the 'month' string to values and then assigning the month name to it, using if-else conditions.
  6. Lastly, I'm adding the three strings as per the order I want.

Android Studio says: java.lang.ArrayIndexOutOfBoundsException: length=2; index=5 java.lang.ArrayIndexOutOfBoundsException: length=2; index=5

Here is my code: ('temp' is the string containing the date)

//TODO: Here lies the method which will apparently convert the movie string to human read-able format...... Use it with caution.
    char release[] = temp.toCharArray();
    Log.d("LOG", release.toString());

    char date[] = new char[2];
    char year[] = new char[4];
    char month[] = new char[2];

    for (int i = 0; i < 11; i++) {

        try {
            if (i > 4 && i < 7) {
                month[i] = release[i];
            }

            if (i < 4) {
                year[i] = release[i];    // ---> Android Studio says error on this line...
            }

            if (i > 7 && i < 10) {
                date[i] = release[i];
            }
        } catch (Error error) {
            error.printStackTrace();
        }
    }

    String monthString = month.toString();
    String dateString = date.toString();
    String yearString = year.toString();
    String finalReleaseDate;

    if (monthString.contentEquals("01")) {
        monthString = "January";
    } else if (monthString.contentEquals("02")) {
        monthString = "February";
    } else if (monthString.contentEquals("03")) {
        monthString = "March";
    } else if (monthString.contentEquals("04")) {
        monthString = "April";
    } else if (monthString.contentEquals("05")) {
        monthString = "May";
    } else if (monthString.contentEquals("06")) {
        monthString = "June";
    } else if (monthString.contentEquals("07")) {
        monthString = "July";
    } else if (monthString.contentEquals("08")) {
        monthString = "August";
    } else if (monthString.contentEquals("09")) {
        monthString = "September";
    } else if (monthString.contentEquals("10")) {
        monthString = "October";
    } else if (monthString.contentEquals("11")) {
        monthString = "November";
    } else if (monthString.contentEquals("12")) {
        monthString = "December";
    }

    finalReleaseDate = dateString + " " + monthString + ", " + yearString;
    movieModel.setReleaseDate(finalReleaseDate);

The message in logcat is: java.lang.ArrayIndexOutOfBoundsException: length=2; index=5 java.lang.ArrayIndexOutOfBoundsException: length=2; index=5 (I have marked the line at which this error is shown..)

Please help!!

You are iterating over i from 0 to 11 release array contains 10 digits only so you should iterate from 0 to 9

Oh Wait that is not your problem, you main problem that year only contains 4 objects from 0 to 3 so you cannot get item i when i is greater that 3

Is that all I guess not... the log cat says the index=5 so the error occurred when i is 5, but the line you are saying is included when i is smaller than 4, So I guess you read the log cat wrong.

And Also it says the length is 2 so it crashed when reading either date array or month array but no way when it read the year array

I brief as @Ken Wolf Wolf in the comment you can use SimpleDateFormat

SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-mm-dd", Locale.getDefault());
Date date = simpleDate.parse("");
simpleDate = new SimpleDateFormat("dd MMMM, yyyy.", Locale.getDefault());
String newFormatString = simpleDate.format(date);

First , you method is not the best way to convert number month to string month . It should be easier and more stable to convert it by using Date and SimpleDateFormat method.


Second , you have marked the wrong line for the crash. This crash (java.lang.ArrayIndexOutOfBoundsException: ***length=2; index=5***) tells you that index is 5 and array length is 4 .

if (i < 4) {
   year[i] = release[i];  //index 5 cannot be called here and year lenght is 4
}
if (i > 4 && i < 7) {
   month[i] = release[i]; //index 5 is called here and month lenght is 2
}
if (i > 7 && i < 10) {
   date[i] = release[i];
}

Crash is happening because you are calling month[5] = release[5]; and month length is only 2.

To avoid this change your code to:

if (i<4) {
    //2015 index  0,1,2,3
    year[i] = release[i];
} else if (i>4 && i<7) {
    //04 index  5,6
    month[i-5] = release[i];
} else if (i>7 && i<10) {
    //10 index 8,9
    date[i-8] = release[i];
}

Third , if you want to get year, month, day in this way it is easier in this way.

String [] date_splited = server_string.split("-");
String year  = date_splited[0];
String month = date_splited[1];
String day   = date_splited[2];

Hope it helps you.

The problem is not occuring at the place that you say. It probably occurs at the first or the thrid if statement. Because date[] and month[] are both only 2 in length, and you are trying to access that with index higher than 2.

The proper way should be using SimpleDateFormat as Omar marked. Once you have a Date (or a Calendar maybe depending the use you will give to the information) you can use the local settings to translate it.

why you use char[] ? you can use String[] directly. In string there is split method available. So using split method you can separate date month and year. You can use below code:

String date=  "2015-04-10";
String[] temp= date.split("-");

temp[0] is 2015, temp[1] is 04 and temp[1] is 10.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM