博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
不用系统函数将字符串转换成整型【Java算法】
阅读量:4541 次
发布时间:2019-06-08

本文共 1927 字,大约阅读时间需要 6 分钟。

1.判断字符串是否为空;

2.判断字符串是否为空字符串;
3.判断是否为负数;
4.从左向右循环取字符,转化为数字,将上次结果乘10,加上当前数字;
5.在运算前,判断结果是否超出int型范围。
MyParseInt.java:

01.import java.util.*;

02. public class MyParseInt {
03.
04.     public static int myParseInt(String str) throws NumberFormatException {
05.        //存放结果
06.         int result = 0;
07.        //标记是否为负数
08.         boolean negative = false;
09.        //数字开始位置:正数为0,负数为1(除去‘-’)
10.         int start = 0;
11.        //int型边界值的十位以上的数字
12.         int limitLeft;
13.        //int型边界值的个位数字
14.         int limitRight;
15.        //判断是否为空
16.         if (str == null) {
17.             throw new NumberFormatException("null");
18.        }
19.         int len = str.length();
20.        //判断是否为空字符串
21.         if (len < 1) {
22.             throw new NumberFormatException("empty");
23.         } else {
24.             if (str.charAt(0) == '-') {
25.                 if (len > 1) {
26.                    //负数
27.                    negative = true;
28.                    start = 1;
29.                    limitLeft = -(Integer.MIN_VALUE / 10);
30.                    limitRight = -(Integer.MIN_VALUE % 10);
31.                 } else {
32.                     throw new NumberFormatException("not number");
33.                }
34.             } else {
35.                limitLeft = Integer.MAX_VALUE / 10;
36.                limitRight = Integer.MAX_VALUE % 10;
37.            }
38.        }
39.        //从左向右取字符,转化为数字,将上次结果乘10,加上当前数字
40.         for (int i = start; i < len; i++) {
41.             char c = str.charAt(i);
42.            //判断是否为数字
43.             if (c < 48 || c > 57) {
44.                 throw new NumberFormatException("not number");
45.             } else {
46.                 int value = c - 48;//减去0的Ascii码 (从左向右依次取) 47.                //在运算前,判断结果是否超出int型范围
48.                 if (result > limitLeft || (result == limitLeft && value >= limitRight + 1)) {
49.                     throw new NumberFormatException("number is out of bounds");
50.                 } else {
51.                     result = result *10 + value;
52.                }
53.            }
54.        }
55.         if (negative) {
56.            result = -result;
57.        }
58.         return result;
59.    }
60.
61.     public static void main(String[] args) {
62.         while (true) {
63.             Scanner sc = new Scanner(System.in);
64.            String str = sc.next();
65.             try {
66.                System.out.println(MyParseInt.myParseInt(str));    
67.            }
68.             catch (NumberFormatException e) {
69.                e.printStackTrace();
70.            }    
71.        }
72.    }
73.}

转载于:https://www.cnblogs.com/youliang/p/3503713.html

你可能感兴趣的文章
Linux 下查看系统是32位 还是64 位的方法
查看>>
MySQL 引擎 和 InnoDB并发控制 简介
查看>>
Dave Python 练习二
查看>>
第二章 第五节 获取帮助
查看>>
关于源代码及其管理工具的总结
查看>>
此文对你人生会有莫大好处的,建议永久保存 2013-07-26 11:04 476人阅读 评论(0) ...
查看>>
JQuery怎样返回前一页
查看>>
Best Time to Buy and Sell Stock
查看>>
Web服务器的原理
查看>>
记录ok6410 jlink 命令行调试uboot
查看>>
ASP.net 内置对象
查看>>
Docker快速配置指南
查看>>
Python基础---OS模块 (二)
查看>>
【JS点滴】substring和substr以及slice和splice的用法和区别。
查看>>
awk多模式匹配
查看>>
线段树
查看>>
a span等行内元素加margin属性后无效果解决方案
查看>>
傻瓜式硬盘重装win7系统图文加视频教程
查看>>
BZOJ 1607 [Usaco2008 Dec]Patting Heads 轻拍牛头:统计 + 筛法【调和级数】
查看>>
如果一个人请优雅的活着。
查看>>