可以用抛异常的方法解决 String num="0123456789" String x="abc123" try{ int n=Integer.parseInt(num); int n=Integer.parseInt(x); }catch(NumberFormatException){ System.out.println("not number"); }
或者用Pattern.matches("[0-9]+",String str);
Pattern.matches("\\d+",String str);Pattern类的matches方法如下: static boolean matches(String regex, CharSequence input) Compiles the given regular expression and attempts to match the given input against it.
偶不会
只有用笨方法了
String num="0123456789"
String x="abc123"
String z="this is number";
for(int i=0;i<x.getbytes.length;i++){
String y=String.valueOf(x.charAt(i));
if(num.indexof(y)==-1){
z="not number";
}
}
System.out.println(z);}
先导入java.util.regex.*
然后用Pattern.matches("\\d+",String str);
str是你要判断的式子
"\\d+"表是一个或多个数字
返回值为boolean
String num="0123456789"
String x="abc123"
try{
int n=Integer.parseInt(num);
int n=Integer.parseInt(x);
}catch(NumberFormatException){
System.out.println("not number");
}
static boolean matches(String regex, CharSequence input)
Compiles the given regular expression and attempts to match the given input against it.