代码如下:
package test;
import java.util.regex.Pattern;
/**
* 判断字符串是否是double数(实数)
*
*/
public class Test9 {
public static void main(String[] args) {
String[] arr= {"3.14","-5.0","+5","123","0","-0.0001","0001","0.0000","0.0a","-b.00","985","3.1415982","14000000000","3.14.14","192.168.1.1"};
for(String s:arr) {
String rt=isDouble(s)?"是":"不是";
System.out.println(s+""+rt+"double数");
}
}
public static boolean isDouble(String s) {
Pattern pattern = Pattern.compile("[+-]?\\d+(.\\d+)?");
return pattern.matcher(s).matches();
}
}
输出:
3.14是double数
-5.0是double数
+5是double数
123是double数
0是double数
-0.0001是double数
0001是double数
0.0000是double数
0.0a不是double数
-b.00不是double数
985是double数
3.1415982是double数
14000000000是double数
3.14.14不是double数
192.168.1.1不是double数
END