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

java判断变量是不是数字方法总结

2024/3/17 2:19:24发布16次查看
java判断变量是不是数字方法:(相关视频课程推荐:java视频教程)
1、用java自带的函数
public static boolean isnumeric(string str){for (int i = 0; i < str.length(); i++){ system.out.println(str.charat(i)); if (!character.isdigit(str.charat(i))){ return false; }}return true;}
2、用正则表达式
首先要import java.util.regex.pattern 和 java.util.regex.matcher
public boolean isnumeric(string str){ pattern pattern = pattern.compile("[0-9]*"); matcher isnum = pattern.matcher(str); if( !isnum.matches() ){ return false; } return true;}
3、使用org.apache.commons.lang
org.apache.commons.lang.stringutils;boolean isnunicodedigits=stringutils.isnumeric("aaa123456789");http://jakarta.apache.org/commons/lang/api-release/index.html下面的解释:isnumericpublic static boolean isnumeric(string str)checks if the string contains only unicode digits. a decimal point is not a unicode digit and returns false.null will return false. an empty string ("") will return true.stringutils.isnumeric(null) = falsestringutils.isnumeric("") = truestringutils.isnumeric(" ") = falsestringutils.isnumeric("123") = truestringutils.isnumeric("12 3") = falsestringutils.isnumeric("ab2c") = falsestringutils.isnumeric("12-3") = falsestringutils.isnumeric("12.3") = false
parameters:str - the string to check, may be nullreturns:true if only contains digits, and is non-null
上面三种方式中,第二种方式比较灵活。
第一、三种方式只能校验不含负号“-”的数字,即输入一个负数-199,输出结果将是false;
而第二方式则可以通过修改正则表达式实现校验负数,将正则表达式修改为“^-?[0-9]+”即可,修改为“-?[0-9]+.?[0-9]+”即可匹配所有数字。
如果我输入的是"a“,它能识别出来这个不是数字,该怎么写?
import java.io.* ;import java.util.* ;public class test{public static void main(string [] args) throws exception{system.out.println("请输入数字:");bufferedreader br=new bufferedreader(new inputstreamreader(system.in)); string line=br.readline(); if(line.matches("\\d+")) //正则表达式 详细见java.util.regex 类 pattern system.out.println("数字"); else system.out.println("非数字");}}
("\d")是数字0-9 ("\\d+")是什么意思?
正则表达式用两个斜杠表示一个斜杠,后面跟着一个加号表示出现一次或多次,完整的意思就是整个字符串中仅包含一个或多个数字。
4、判断ascii码值
public static boolean isnumeric0(string str){ for(int i=str.length();--i>=0;){ int chr=str.charat(i); if(chr<48 || chr>57) return false; } return true; }
5、逐个判断str中的字符是否是0-9
public static boolean isnumeric3(string str){ final string number = "0123456789"; for(int i = 0;i if(number.indexof(str.charat(i)) == -1){ return false; } } return true; }
6、捕获numberformatexception异常
public static boolean isnumeric00(string str){ try{ integer.parseint(str); return true; }catch(numberformatexception e){ system.out.println("异常:\"" + str + "\"不是数字/整数..."); return false; } }
ps:不提倡使用方法6,原因如下:
1、numberformatexception是用来处理异常的,最好不要用来控制流程的。
2、虽然捕捉一次异常很容易,但是创建一次异常会消耗很多的系统资源,因为它要给整个结构作一个快照。
看一下jdk源码:
public static long parselong(string s,int radix) throws numberformatexception { if(s == null){ throw new numberformatexception("null"); } if(radix < character.min_radix){ throw new numberformatexception("radix " + radix + " less than character.min_radix"); } if(radix > character.max_radix){ throw new numberformatexception("radix " + radix + " greater than character.max_radix"); } long result = 0; boolean negative = false; int i = 0,max = s.length(); long limit; long multmin; int digit; if(max > 0){ if(s.charat(0) == '-'){ negative = true; limit = long.min_value; i++; }else{ limit = -long.max_value; } multmin = limit / radix; if(i < max){ digit = character.digit(s.charat(i++),radix); if(digit < 0){ throw new numberformatexception(s); }else{ result = -digit; } } while(i < max){ // accumulating negatively avoids surprises near max_value digit = character.digit(s.charat(i++),radix); if(digit < 0){ throw new numberformatexception(s); } if(result < multmin){ throw new numberformatexception(s); } result *= radix; if(result < limit + digit){ throw new numberformatexception(s); } result -= digit; } }else{ throw new numberformatexception(s); } if(negative){ if(i > 1){ return result; }else{ throw new numberformatexception(s); } }else{ return -result; } }
可以看出来jdk里也是一个字符一个字符的判断,如果有一个不是数字就抛出numberformatexception,所以还不如这个工作由我们自己来做,还省得再抛出一次异常。
更多java相关文章请关注java基础教程。
以上就是java判断变量是不是数字方法总结的详细内容。
该用户其它信息

VIP推荐

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