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;
}
狸猫的笔试——字符转int
最新推荐文章于 2024-02-25 13:04:03 发布