Could you find the error? Java implementation


  • 0
    Q
    public int compareVersion(String version1, String version2) {
    	
    	double ver1 = str2float(version1);
    	double ver2 = str2float(version2);
    	
    	if(ver1 > ver2) {
    		return 1;
    	} else if(ver1 < ver2) {
    		return -1;
    	} else {
    		return 0;
    	}		
    }
    
    public double str2float(String str) {
    	double sum = 0.0;
    	String[] parts = str.split("\\.");
    	for(int i = 0; i < parts[0].length(); i++) {
    		sum += (Integer.valueOf(parts[0].charAt(i))-48)* Math.pow(10, parts[0].length()-i-1);
    	}
    	
    	for(int j = 0; j < parts[1].length(); j++) {
    		sum += (Integer.valueOf(parts[1].charAt(j))-48)* Math.pow(10, -(j+1));
    	}
    
    	return sum;
    }
    

    Runtime Error Message: Line 23: java.lang.ArrayIndexOutOfBoundsException: 1
    Last executed input: "1", "0"


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.