java实现atol函数_Java实现atoi

atoi(ascii to integer)将字符串转换成整型数,Java中提供了Integer.parseInt方法将字符串转换成数字,那么又如何自行实现呢?

需考虑处理以下情况

以正负符号开头的字符串,如"-123","+120"

以多个零字符开头的字符串,如"00002123"

转换的数字大于Integer.MAX_VALUE或小于Integer.MIN_VALUE

假如字符串除开头以外,任意一位字符的值小于'0'或大于'9',即可判断为异常

空字符

Talk is cheap,show me the code

public class Atoi {

public static int parseStringToInt(String str) {

//空字符抛出异常

if ("".equals(str) || "null".equals(str) || str == null) {

throw new RuntimeException("empty");

}

//将字符串转换成字符数组

char[] chars = str.toCharArray();

//保存计算结果

int result = calculateTotal(chars);

//大于Integer.MAX_VALUE时

if (result >= Integer.MAX_VALUE) {

return Integer.MAX_VALUE;

}

//小于Integer.MIN_VALUE时

if (result <= Integer.MIN_VALUE) {

return Integer.MIN_VALUE;

}

return result;

}

private static int calculateTotal(char[] chars){

char first = chars[0];

//保存计算结果

int result = 0;

if (first == '-' || first == '+') {

//由于该字符串以正负开头,所以计算数值应该从第二位开始

result = calculate(chars, 1);

//转换成负数

if (first == '-') {

result = -result;

}

} else {

//从第一位开始计算字符

result = calculate(chars, 0);

}

return result;

}

/**

* 计算字符串数字的整型值

*

* @param chars 字符串数字

* @param start 开始位置 即 '12345' 开始位置为3的话

* @return

*/

private static int calculate(char[] chars, int start) {

if (chars == null || chars.length == 0) {

throw new RuntimeException("字符串数组不能为空");

}

if (chars.length < start) {

throw new RuntimeException("start值不能大于数组长度");

}

//保存计算结果

int result = 0;

//得到移除0开头后的字符数组

char[] vailChar = removeHeaderZero(chars);

for (int i = start; i < vailChar.length; i++) {

if (vailChar[i] < '0' || vailChar[i] > '9') {

throw new FormatException(String.valueOf(chars)+"含有不合法字符'"+chars[i]+"'");

} else {

//转换成对应的整型

int value = vailChar[i] - '0';

//位数

int j = vailChar.length - i - 1;

//计算字符所在位数的数值,如2在百位的时候,就需要乘两次10

while (j > 0) {

value = value * 10;

j--;

}

result += value;

}

}

return result;

}

/**

* 获取正常的数字字符串 如0开头的就将它移除

*

* @param chars

* @return

*/

static private char[] removeHeaderZero(char[] chars) {

//初始化只有一个元素为'0'的数组,避免chars 所有元素都是0的情况

char[] result = {'0'};

boolean isNotZeroFlag = true;

//计算第一个非零数字的位置

int indexOfNoZero = 0;

for (int i = 0; i < chars.length; i++) {

if (chars[i] == '0' && isNotZeroFlag) {

indexOfNoZero++;

} else {

if (chars[i] < '0' || chars[i] > '9') {

throw new FormatException(String.valueOf(chars)+"含有不合法字符'"+chars[i]+"'");

}

//结束

isNotZeroFlag = false;

}

//得到第一个非零数字的位置后

if (!isNotZeroFlag) {

//创建新的数组

result = new char[chars.length - indexOfNoZero];

for (int j = 0; j < result.length; j++) {

result[j] = chars[j + indexOfNoZero];

}

break;

}

}

return result;

}

private static class FormatException extends RuntimeException{

public FormatException(String message) {

super(message);

}

}

//1-2+3/4+5*6+(1+2+3)

public static int calculateWithString(String str){

return 0;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值