Tuesday, February 11, 2014

Write a program to convert string to number without using Parse method?

public class MyStringToNumber {

    public static int convert_String_To_Number(String numStr){
        
        char ch[] = numStr.toCharArray();
        int sum = 0;
        int zeroAscii = (int)'0';
        for(char c:ch){
            int tmpAscii = (int)c;
            sum = (sum*10)+(tmpAscii-zeroAscii);
        }
        return sum;
    }
    
    public static void main(String[] args){
        System.out.println("\"3256\" == "+convert_String_To_Number("3256"));
    }
}

No comments:

Post a Comment