java中的String转int需要注意的问题
来源:互联网 发布:淘宝售后服务怎么做 编辑:程序博客网 时间:2024/05/22 01:38
1。null,”“,(空,空串)
2。满足:首字符为+,-,数字,
不满足:其它的例如字母,空格串
3。溢出(先用long获取,若不满足,则返回错误)
具体代码:
package StringExercise;/** * @author wangpei * @version 创建时间:2017年3月24日 上午12:16:50 类说明 */public class StringToInt {// 字符串转整数 static String s = " "; static boolean b = true; public static void main(String[] args) {// 对所有可能出现的情况加以考虑 int result = change(s); if (b == false) System.out.println("输入不正确"); else System.out.println("转换后:" + result); } public static int change(String str) { System.out.println(str.isEmpty()); if (s == null || s != null && s.equals("")) {// 输入非法结束。 b = false; return 0; } if (str.charAt(0) == '-' || str.charAt(0) == '+' || (str.charAt(0) >= '0' && str.charAt(0) < '9')) {// 负数 long l = IsOutOfMax(str); return (int) l; } else {// 非法输入,直接结束 b = false; return 0; } } public static long IsOutOfMax(String s) { long l = 0; l = Long.parseLong(s); if (l < -2147483648 || l > 2147483647) { b = false; return 0; } else return l; }}
定义boolean类型的b作用:判断返回的0是输入错误的0,还是转换来的0;
0 0
- java中的String转int需要注意的问题
- int 转 DateTime 需要注意的问题
- js string 转 int 注意的问题
- js string 转 int 注意的问题
- String: 需要注意的问题
- 对java中的String采用+=连接字符串需要注意的一个问题
- 对java中的String采用+=连接字符串需要注意的一个问题
- js string 转 int 注意的问题小结
- String类型转Long类型需要注意的问题
- css中的overflow需要注意的问题
- realloc()使用中的需要注意的问题
- java Date(int year,int month,int date)需要注意
- java中的String 与 int 的转换
- Java中的一些需要注意的地方
- javascript——js string 转 int 注意的问题——parseInt
- javascript——js string 转 int 注意的问题——parseInt
- javascript——js string 转 int 注意的问题——parseInt
- js中 string 转 int 注意的问题——parseInt
- 关于前端开发谈谈单元测试
- Android Study之跳转自启动管理页
- 查看linux中常用端口
- UnityShader官方案例讲解——SurfaceShader(4)
- 电脑技术:win10关机后电脑自动重启解决方法
- java中的String转int需要注意的问题
- http请求和响应格式
- Unable to chmod sdcard: Read-only file system安卓原生模拟器中的解决办法
- mac系统下快速搭建apache+php+mysql,并启用mac自带svn客户端
- 如何使用PowerMock帮助做TDD?
- ros::spin()和ros::spinOnce()的意义与区别
- codeforces#406 A (拓展欧几里得水题)
- win8环境下删除Cygwin
- C++ 的反思