「java判断全是数字」java判断数字是几位数

博主:adminadmin 2023-03-22 19:57:07 657

今天给各位分享java判断全是数字的知识,其中也会对java判断数字是几位数进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java中判断字符串是否为纯数字

//方法一:用JAVA自带的函数\x0d\x0apublic static boolean isNumeric(String str)\x0d\x0a{ for (int i = str.length();--i=0;)\x0d\x0a{\x0d\x0aif (!Character.isDigit(str.charAt(i)))\x0d\x0a{ \x0d\x0areturn false;6 \x0d\x0a}\x0d\x0a}\x0d\x0areturn true;\x0d\x0a}\x0d\x0a\x0d\x0a/*方法二:推荐,速度最快\x0d\x0a* 判断是否为整数 \x0d\x0a* @param str 传入的字符串 \x0d\x0a* @return 是整数返回true,否则返回false \x0d\x0a*/\x0d\x0apublic static boolean isInteger(String str) { \x0d\x0aPattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); \x0d\x0areturn pattern.matcher(str).matches(); \x0d\x0a}\x0d\x0a//方法三:public static boolean isNumeric(String str){\x0d\x0aPattern pattern = Pattern.compile("[0-9]*"); return pattern.matcher(str).matches(); \x0d\x0a}\x0d\x0a\x0d\x0a//方法四:public final static boolean isNumeric(String s) { if (s != null !"".equals(s.trim())) return s.matches("^[0-9]*$"); else\x0d\x0areturn false;\x0d\x0a} \x0d\x0a//方法五:用ascii码 public static boolean isNumeric(String str){ for(int i=str.length();--i=0;){ int chr=str.charAt(i); if(chr57) return false;\x0d\x0a} return true;\x0d\x0a}

Java 中怎样判断一个字符串全是数字

Java中判断字符串是否全是数字:

可以使用正则表达式:

public boolean isNumeric(String str) {  

        Pattern pattern = Pattern.compile("[0-9]*");  

        Matcher isNum = pattern.matcher(str);  

        if (!isNum.matches()) {  

            return false;  

        }  

        return true;  

    }

但是这个方法并不安全,没有对字符串进行空校验。 

在程序执行的时候很容易抛出异常。 

例如执行:

public static void main(String[] args) {  

      

        String str = null;  

        System.out.println(BarcodeChecksum.INSTANCE.isNumeric(str));  

  

    }

就会抛出异常:

Exception in thread "main" java.lang.NullPointerException  

    at java.util.regex.Matcher.getTextLength(Matcher.java:1140)  

    at java.util.regex.Matcher.reset(Matcher.java:291)  

    at java.util.regex.Matcher.init(Matcher.java:211)  

    at java.util.regex.Pattern.matcher(Pattern.java:888)  

    at com.ossez.bcu.util.BarcodeChecksum.isNumeric(BarcodeChecksum.java:37)  

    at com.ossez.bcu.util.BarcodeChecksum.main(BarcodeChecksum.java:53)

所以这个方法并不准确。 

如果执行:

public static void main(String[] args) {  

        String str = "";  

        System.out.println(BarcodeChecksum.INSTANCE.isNumeric(str));  

    }

将会返回 true。 

这说明这个方法没有对空字符串进行校验。 

可以使用 Apache 的 StringUtils.isNumeric() 函数进行判断。 

这个函数位于 org.apache.commons.lang.StringUtils;  中。 

但是,需要注意,如果传入参数为 "" 同样也会你存在判断不准确的情况,这时候需要首先对需要进行判断的参数进行非空校验,然后删除传入数据中的空格。

public static void main(String[] args) {  

        String str = "";  

        System.out.println(StringUtils.isNumeric(str));  

    }

上面这个函数将会返回 true。 

请先 trim 数据

java中怎么判断字符串是否全部为数字

1 //方法一:用JAVA自带的函数

2 public static boolean isNumeric(String str){

3 for (int i = str.length();--i=0;){

4 if (!Character.isDigit(str.charAt(i))){

5 return false;

6 }

7 }

8 return true;

9 }

/*方法二:推荐,速度最快

* 判断是否为整数

* @param str 传入的字符串

* @return 是整数返回true,否则返回false

*/

public static boolean isInteger(String str) {

Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");

return pattern.matcher(str).matches();

}

//方法三:

public static boolean isNumeric(String str){

Pattern pattern = Pattern.compile("[0-9]*");

return pattern.matcher(str).matches();

}

//方法四:

public final static boolean isNumeric(String s) {

if (s != null !"".equals(s.trim()))

return s.matches("^[0-9]*$");

else

return false;

}

//方法五:用ascii码

public static boolean isNumeric(String str){

for(int i=str.length();--i=0;){

int chr=str.charAt(i);

if(chr48 || chr57)

return false;

}

return true;

java中怎样判断一个字符串全是数字?

1.使用Character.isDigit(char)判断

char num[] = str.toCharArray();//把字符串转换为字符数组

StringBuffer title = new StringBuffer();//使用StringBuffer类,把非数字放到title中

StringBuffer hire = new StringBuffer();//把数字放到hire中

for (int i = 0; i num.length; i++) {

// 判断输入的数字是否为数字还是字符

if (Character.isDigit(num[i])) {把字符串转换为字符,再调用Character.isDigit(char)方法判断是否是数字,是返回True,否则False

hire.append(num[i]);// 如果输入的是数字,把它赋给hire} else {title.append(num[i]);// 如果输入的是字符,把它赋给title}}}

2.使用类型转换判断try {String str="123abc";

int num=Integer.valueOf(str);//把字符串强制转换为数字

return true;//如果是数字,返回True

} catch (Exception e) {

return false;//如果抛出异常,返回False}

3.使用正则表达式判断

String str = "";

boolean isNum = str.matches("[0-9]+");

//+表示1个或多个(如"3"或"225"),*表示0个或多个([0-9]*)(如""或"1"或"22"),?表示0个或1个([0-9]?)(如""或"7")

ps:这个方法只能用于判断是否是正整数

关于java判断全是数字和java判断数字是几位数的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。