您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

java判断是否为一个数字

2025/11/11 5:14:38发布34次查看
java判断是否是数字的方法:
1、用java自带的函数 
public static boolean isnumeric(string str){ for (int i = str.length();--i>=0;){ if (!character.isdigit(str.charat(i))){ return false; } } return true; }
2、用正则表达式
public static boolean isnumeric(string str){ pattern pattern = pattern.compile("[0-9]*"); return pattern.matcher(str).matches(); }
3、用ascii码
public static boolean isnumeric(string str){ for(int i=str.length();--i>=0;){ int chr=str.charat(i); if(chr<48 || chr>57) return false; } return true; }
4、捕获numberformatexception异常
public static boolean isnumeric00(string str){ try{ integer.parseint(str); return true; }catch(numberformatexception e) { system.out.println("异常:\"" + str + "\"不是数字/整数..."); return false; }}
更多java知识请关注java基础教程。
以上就是java判断是否为一个数字的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product