Before comparing the client version number, always use String.CompareTo, know that the bug has not understood its imperfect place. Its comparison is performed according to the comparison of strings, so it has an incorrect place. For example, the previous client version number is: 9.9.9, and the server-side latest client version number is : 10.0.1, although 10.0.1 is significantly higher than 9.9.9, but according to the CompareTo method, this 9.9.9 is greater than 10.0.1, resulting in a client version number comparison error. The following is a separated version number converted to an integer value to compare, however, the number of the first comparison
1 Public Static intcompareversion (String version1, String version2) {2 if(Version1.equals (Version2)) {3 return0;4 }5string[] Version1array = Version1.split ("//."));6string[] Version2array = Version2.split ("//."));7 intindex = 0;8 //get Minimum Length value9 intMinlen =math.min (Version1array.length, version2array.length);Ten intdiff = 0; One //loop to determine the size of each bit A while(Index < Minlen && (diff = integer.parseint (version1array[index))-Integer.parseint (Version2array[index])) = = 0) { -index++; - } the if(diff = = 0) { - //if the number of digits is inconsistent, compare the number of extra digits - for(inti = index; i < version1array.length; i++) { - if(Integer.parseint (version1array[i]) > 0) { + return1; - } + } A at for(inti = index; i < version2array.length; i++) { - if(Integer.parseint (version2array[i]) > 0) { - return-1; - } - } - return0; in}Else { - returnDiff > 0? 1:-1; to } +}
Comparison of Java implementation version numbers