java中判断字符是不是数字问题?

[复制链接]
查看11 | 回复2 | 2007-11-6 21:22:28 | 显示全部楼层 |阅读模式
参考如下代码即可发现:publicclassT{publicstaticvoidmain(String[]args){System.out.println(isInt("12345"));System.out.println(isInt("abc"));System.out.println(isInt2("12345"));System.out.println(isInt2("abc"));}publicstaticbooleanisInt(Stringstring){returnstring.matches("\\d+");}publicstaticbooleanisInt2(Stringstring){try{Integer.parseInt(string);returntrue;}catch(NumberFormatExceptione){returnfalse;}}}
回复

使用道具 举报

千问 | 2007-11-6 21:22:28 | 显示全部楼层
你特别判断0开头的,把0或0x去了,就可以用Integer的parseInt(String s,int radix)的方法了,如先去掉0x,再用Integer.parseInt("AA",10),如果没有异常就是数字了,否则就不是,还有对我以前的答题错误表示抱歉,以前我的parseInt方法的参数记错了。另外欢迎访问我的个人网站:junqing124.vip2.upftp.com
回复

使用道具 举报

千问 | 2007-11-6 21:22:28 | 显示全部楼层
public static boolean isNumberic(String str) {if (str == null) {return false;}int sz = str.length();for (int i = 0; i ='0'&&ch<='9'){//数字}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行