狸猫的笔试——字符转int

来源:互联网 发布:链轮链条设计软件 编辑:程序博客网 时间:2024/04/28 01:01
public static int getInt(String s,int radix) throws Exception{if(s == null || "".equals(s)||radix > 10||radix < 1){throw new Exception("input error");}int len = s.length();char firstChar = s.charAt(0);boolean flag = false;int result = 0;int i = 0;if(firstChar < '0'){if(len == 1){throw new Exception("input error");}if(firstChar == '-'){flag = true;limit = -Integer.MAX_VALUE;}else if( firstChar != '+'){throw new Exception("s is not number");}i++;}while(i < len){if(s.charAt(i) < '0' || s.charAt(i) > '9'){throw new Exception("s is not number");}result = result * radix + (s.charAt(i++) - '0');}return flag?-result:result;}

0 0
原创粉丝点击