java - Sorted int[] Converted from String[] -- ArrayIndexOutOfBoundsException -
context
i've been trying fix part of program while without success. want sort string []
each element in format: name:number
(i.e. john:32
).
progress
so far, code splits each element , adds equivalent int []
. attempt compare elements in int []
selection sort , swap elements in string []
.
problem
i'm getting java.lang.arrayindexoutofboundsexception string []
, called scores
. why this?
scores = sort(scores); //arrayindexoutofboundsexception here public static string [] sort(string [] a) { //equivalent array containing integer part of score[i] int[] temparray = new int[a.length]; //populate temparray for(int = 0; < a.length; i++) { //acquire numerical part of element //arrayindexoutofboundsexception here******** int num = integer.parseint(a[i].split(":")[1]); //add array temparray[i] = num; } /* selection sort: descending */ //compare elements (integer) in temparray for(int = 0; < temparray.length; i++){ int index = i; //search integers larger above index for(int j = i+1; j < temparray.length; j++ ){ if(temparray[j] > temparray[index]){ index = j; }} //swap elements in scores-array (string) string temp = a[index]; a[index] = a[i]; a[i] = temp; } return a; }
i agree @shmosel
this can rooting out bad apple
string scoresplit[] = a[i].split(":"); if (scoresplit.length() == 2){ int num = integer.parseint(a[i].split(":")[1]); } else{ system.out.println("bad apple "+ scoresplit[0]); //some kind of logging }
Comments
Post a Comment