导航:首页 > 编程语言 > java中判断是否是数字

java中判断是否是数字

发布时间:2023-05-15 09:30:42

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中怎样判断一个字符串是否是数字

用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如何做到判断一个字符串是否是数字。

楼主看方法:
public class NumberDemo {
public static void main(String[] args) {
String str1="1122.2.2";
String str2="111";
String str3="111.2";
String str4="111s";
String str5="111.s";
String str6="1s11";
System.out.println(str1+":"+isNum(str1));
System.out.println(str2+":"+isNum(str2));
System.out.println(str3+":"+isNum(str3));
System.out.println(str4+":"+isNum(str4));
System.out.println(str5+":"+isNum(str5));
System.out.println(str6+":"+isNum(str6));
}
public static boolean isNum(String str){
return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
}
}
结果:
1122.2.2:false
111:true
111.2:true
111s:false
111.s:false
1s11:false

④ java中判断字符串是否数字的两种方法

判断字符串是不是数字,大家可能会用一些java自带的方法,也有可能用其他怪异的招式,比如判断是不是整型数字,将字符串强制转换成整型,不是数字的就会抛出错误,那么就不是整型的了。但本文介绍的比较好的两种方法:
1。java类库自带的方法:
public boolean isNum(String msg){
if(java.lang.Character.isDigit(msg.charAt(0))){
return true;}return false;}�0�2�0�2更新:发现以上方法写得不够到位,现在就改为下面的简单说明了,至于具体的方法实现字符串判断是否数字就不写了。
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;}�0�2
3。用正则表达式

⑤ Java怎样判断输入是否为数字

你可以用try{}catch来处理,如果转换的时候出错了,那就肯定不是数字

⑥ 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]+”即可匹配所有数字。

⑦ java中怎么判断数字

java中判断一个字符是否为数字,可以通过Integer类的方法来判断,如果抛出异常,则不是数字,如下例子:

可以用异常来做校验
/**
*判断字符串是否是整数
*/
publicstaticbooleanisInteger(Stringvalue){
try{
Integer.parseInt(value);//判断是否为数字
returntrue;
}catch(NumberFormatExceptione){//抛出异常
returnfalse;
}
}
阅读全文

与java中判断是否是数字相关的资料

热点内容
有溯源码一定保真吗 浏览:997
云服务器建设方案 浏览:986
jquery源码讲解 浏览:280
宝马app如何发帖 浏览:861
重庆服务器托管商云空间 浏览:439
浦发银行app如何调流水 浏览:677
玉石鉴赏pdf 浏览:842
为什么小度APP一直连不上网络 浏览:163
pdf模板java 浏览:40
现代瑞纳的压缩比 浏览:128
网吧里的ftp服务器有什么用 浏览:872
程序员年终总结工作体会 浏览:153
pdf可以直接打印 浏览:661
android刷wp8 浏览:912
历史地图集pdf 浏览:926
快手app极速版怎么扫码 浏览:805
qq程序员玩法 浏览:96
1是什么门电路app 浏览:867
博之轮运动手表用什么app 浏览:646
asp视频聊天源码 浏览:85