❶ java 判断是否为数字
用正则判断太麻烦,涉及到int、long、float、double等等
给你个通用版的
importjava.math.BigDecimal;
publicclass${
publicstaticvoidmain(String[]args){
System.out.println(isNum("1"));
System.out.println(isNum("1.1"));
System.out.println(isNum("1a"));
}
privatestaticbooleanisNum(Stringstr){
try{
newBigDecimal(str);
returntrue;
}catch(Exceptione){
returnfalse;
}
}
}
❷ java中判断字符串是不是数字
调用string.toCharArray方法,把字符串转化成字符数组。然后用for循环判断其中每一个成员是不是数字就行了。 char[i] 大于等于0 小于等于9 之间的都是数字。
❸ java中怎么判断指定的数据是字符串是否是数字
java中判断字符串是否为数字的方法:
1.用JAVA自带的函数
public static boolean isNumeric(String str){for (int i = 0; i < str.length(); 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; }
❹ Java怎样判断输入是否为数字
你可以用try{}catch来处理,如果转换的时候出错了,那就肯定不是数字
❺ java判断是否是数字
你可以用try{}catch(){}异常机制,把你传入的字符串转换成数字,如果发生异常就不是数字,没有发生异常则是数字
❻ Java中怎样判断一个字符串是否是数字
用java的异常机制,不仅可以判断是否是数字,还可以判断整数或者小数:
public void checkInt(String bh){
try{
int num = Integer.parseInt(bh);//将输入的内容转换成int
System.out.println("是整数:"+num);//是整数
}catch (NumberFormatException e) {//转换成int类型时失败
try{
double d =Double.parseDouble(bh);//转成double类型
System.out.println("是小数:"+d);//是小数
}catch (NumberFormatException e2) {//转成double类型失败
System.out.println("不是数字");
}
}
}
❼ java怎么判断一个字符串是否是数字
如果只是判断,可与用integer.parseint(string)如果是数字,就没有异常,如果有异常,就不是数字
或者用正则表达式
return
string.matches("\\d+\\.?\\d*"));
这个语句就是用来判断的
\\d+表示一个或者多个数字
\\.?
表示一个或这没有小数点
\\d
*
表示0个或者多个数字
❽ java中验证字符串是不是数字的四种方法
判断字符串是不是数字,大家可能会用一些java自带的方法,也有可能用其他怪异的招式,比如判断是不是整型数字,将字符串强制转换成整型,不是数字的就会抛出错误,那么就不是整型的了。但本文介绍的比较好的两种方法:
1。java类库自带的方法:
public boolean isNum(String msg){
if(java.lang.Character.isDigit(msg.charAt(0))){
return true;}return false;}0202更新:发现以上方法写得不够到位,现在就改为下面的简单说明了,至于具体的方法实现字符串判断是否数字就不写了。
java.lang.Character.isDigit(char ch) boolean
isDigit 只能作用于char,所以判断字符串是否为数字,要一个一个拿出char进行判断。
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;}02
3。用正则表达式
❾ Java中怎样判断一个字符串是否是数字
ava中判断字符串是否为数字的方法:
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下面的解释:
isNumeric
public 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) = false
StringUtils.isNumeric("") = true
StringUtils.isNumeric(" ") = false
StringUtils.isNumeric("123") = true
StringUtils.isNumeric("12 3") = false
StringUtils.isNumeric("ab2c") = false
StringUtils.isNumeric("12-3") = false
StringUtils.isNumeric("12.3") = false
Parameters:
str - the String to check, may be null
Returns:
true if only contains digits, and is non-null
上面三种方式中,第二种方式比较灵活。
第一、三种方式只能校验不含负号“-”的数字,即输入一个负数-199,输出结果将是false;
而第二方式则可以通过修改正则表达式实现校验负数,将正则表达式修改为“^-?[0-9]+”即可,修改为“-?[0-9]+.?[0-9]+”即可匹配所有数字。