import java.util.Scanner;
import java.util.regex.Matcher;
public class Test {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
String s = reader.next();
if (s.matches("[\\d]+[\\.]?[\\d]*")) {
System.out.println("是数字");
} else {
System.out.println("不是数字");
}
}
}
首先要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;
}