compile error, really don't know why


  • 0
    C

    I met the compile error all the time and I really dont know why.

    Here is my code

    public class solution {
    public int countDigitOne(int n) {
    int result = 0;
    String everyDigit = new String();

    	//to be more convenient, I added an extra 0 before and after all digits.
    	everyDigit = '0' + String.valueOf(n) + '0';
    	
    	//if n is less than or equal to 0, the number of digit 1 is 0.
    	if(n <= 0) {
    		return 0;
    	}
    	//if n is bigger than 0
    	for(int i = 1; i < everyDigit.length() - 1; i++) {
    		if((int)(everyDigit.charAt(i)-48) > 1) {
    			result = (int) (result + (Integer.valueOf(everyDigit.substring(0, i))+1) * Math.pow(10, everyDigit.length()-i-2));
    		}
    		else if((int)(everyDigit.charAt(i)-48) == 1) {
    			if(i == everyDigit.length()-2) {
    				result = (int) (result + (Integer.valueOf(everyDigit.substring(0, i))) * Math.pow(10, everyDigit.length()-i-2) + 1);
    			}
    			else {
    				result = (int) (result + (Integer.valueOf(everyDigit.substring(0, i))) * Math.pow(10, everyDigit.length()-i-2)
    								+ Integer.valueOf(everyDigit.substring(i+1, everyDigit.length()-1)) + 1);
    			}
    		}
    		else if((int)(everyDigit.charAt(i)-48) < 1) {
    			result = (int) (result + (Integer.valueOf(everyDigit.substring(0, i))) * Math.pow(10, everyDigit.length()-i-2));
    		}
    	}	
    	return result;
    }
    

    }


Log in to reply
 

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