java - IndexOutOfBounds error when trying to get last value -
so suppose loop through array , finding @ points array starts new year , how many days in year. problem have can't last year of array 2013.
2010 [25202, 25567) 365 days.
2011 [25567, 25932) 365 days.
2012 [25932, 26298) 366 days.
exception in thread "main" java.lang.arrayindexoutofboundsexception: 26663 @ weatheranalysis.yearsarray(weatheranalysis.java:213) public static void yearsarray(string a[]) { // array find amnount of days in year int range1 = 0; int range2 = 1; string year = ""; (int = 0; <= a.length-1; i++) { string s = a[i]; string p = a[i+1]; if (s.equals(p)) { range2 ++; }else{ year = s; system.out.println(year+" ["+range1+", "+range2 +") "+ (range2-range1)+" days"); range1 = range2; range2 ++; } } } }
your loop goes a.length-1
. last index in array. however, try access index after when string p = a[i+1]
. trying access i+1
, outside bounds of array.
here's debugging tip: when exception says at weatheranalysis.yearsarray(weatheranalysis.java:213)
, 213
line number exception being thrown from. line string p = a[i+1]
on it.
Comments
Post a Comment