如何在不抛异常的情况下,将一个字符串转换成int

lixiyuyu 2016-08-16 04:45:35
如题,碰到的一个面试题,我的回答是 int num = Integer.valueOf("123456"); 但答案好像不对,所以想听下大家的回答
...全文
500 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovejp2Sec 2016-08-17
  • 打赏
  • 举报
回复
楼上都太复杂了,我觉得就是一个小学题
public class Demo7 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
		int length = s.length();
		int sum = 0;
		int flag = 0;
		for(int i = length;i>0;i--){
			String s2 = s.substring(i-1, i);
			int a = Integer.parseInt(s2);
			sum+=a*Math.pow(10, flag++);
		}
	      System.out.println(sum);
	}

}
codingjav 2016-08-17
  • 打赏
  • 举报
回复
正则表达式进行验证吧
Volcano__Liu 2016-08-17
  • 打赏
  • 举报
回复
引用 6 楼 lovejp2Sec 的回复:
楼上都太复杂了,我觉得就是一个小学题
public class Demo7 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
		int length = s.length();
		int sum = 0;
		int flag = 0;
		for(int i = length;i>0;i--){
			String s2 = s.substring(i-1, i);
			int a = Integer.parseInt(s2);
			sum+=a*Math.pow(10, flag++);
		}
	      System.out.println(sum);
	}

}
输个100000000你程序还对吗?
NewMoons 2016-08-16
  • 打赏
  • 举报
回复
这种题其实现场写出完全正确的代码很难,因为在不能调试的情况下谁也不能保证能够写出正确的正则语法。 其实给出思路即可,我的很多面试其实最后在和面试官沟通中也都是说其实就是看思路。写伪代码也可。 说的白一点,就是如何用正则来校验字符串是否为合法数字。 我觉得这一句话就够了,如果非要你现场写出完全正确的代码(手写不是机器调试),我觉得这公司也没什么前途,技术负责人属于钻牛角尖的类型。
xugy91 2016-08-16
  • 打赏
  • 举报
回复
public static void main(String[] args) {
		String intstr = "123423243";
		String pattern = "^\\d+$";
		//不可为空
		if(intstr==null) {
			System.err.println("不可输入空值");
		}
		//判断是否是非负数
		boolean matches = Pattern.matches(pattern,intstr);
		if(matches) {
			//判断是否超过 Integer最大范围
			if(intstr!=null && intstr.length()>10) {
				System.err.println("您输入的数字已经超过Integer的最大范围");
				return;
			}
			int max_int = Integer.MAX_VALUE;
			long parseLong = Long.parseLong(intstr);
			if(parseLong<(long)max_int) {
				int result = Integer.parseInt(intstr);
				System.out.println("您要的结果为:" + result) ;
			} else {
				System.err.println("您输入的数字已经超过Integer的最大范围");
			}
		} else {
			System.err.println("您输入内容非正整数");
		}
	}
xugy91 2016-08-16
  • 打赏
  • 举报
回复
public static void main(String[] args) { String intstr = "123423243"; String pattern = "^\\d+$"; //不可为空 if(intstr==null) { System.err.println("不可输入空值"); } //判断是否是非负数 boolean matches = Pattern.matches(pattern,intstr); if(matches) { //判断是否超过 Integer最大范围 if(intstr!=null && intstr.length()>10) { System.err.println("您输入的数字已经超过Integer的最大范围"); return; } int max_int = Integer.MAX_VALUE; long parseLong = Long.parseLong(intstr); if(parseLong<(long)max_int) { int result = Integer.parseInt(intstr); System.out.println("您要的结果为:" + result) ; } else { System.err.println("您输入的数字已经超过Integer的最大范围"); } } else { System.err.println("您输入内容非正整数"); } }
nikyotensai 2016-08-16
  • 打赏
  • 举报
回复
int i=Integer.parseInt("123456"); System.out.println(i);
nikyotensai 2016-08-16
  • 打赏
  • 举报
回复
int i=new Integer("123456"); System.out.println(i);

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧