如何判断一个字符串里是否带有数字或数字里有小数点? "123f" 无效"123" 有效"123.12"有效 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我只想知道怎么判断一个String型里面是否全部数字或者数字里有小数点。 没看明白,是判断某个字符串是否是数字(包括小数)?如果是:1.try { double num = Double.parseDouble("123.3"); System.out.println("有效"); } catch (NumberFormatException ex) { System.out.println("无效"); }2. java.util.regex.Pattern p=java.util.regex.Pattern.compile("^\\d+$|^\\d+\\.\\d+$"); java.util.regex.Matcher m=p.matcher("123"); if(m.matches()){ System.out.println("有效"); }else{ System.out.println("无效"); } String str="123"; java.util.regex.Pattern p1=java.util.regex.Pattern.compile("^\\d+$"); java.util.regex.Pattern p2=java.util.regex.Pattern.compile("^\\d+\\.\\d+$"); java.util.regex.Matcher m1=p1.matcher(str); java.util.regex.Matcher m2=p1.matcher(str); if(m1.matches()){ System.out.println("字符串str全是数字!"); }else if(m2.matches()){ System.out.println("字符串str是个含有小数的数字!"); }else{ System.out.println("字符串str不是个有效数字!"); } 谢谢 lip009(深蓝忧郁) 正解 谢谢 lip009(深蓝忧郁) 正解 两个线程打台球的问题 求解决 ...shishi 做J2EE就是做一个网站,这个认为对吗? 关于异常的问题 简单问题 急:请问哪里可以下载thinking in java 的候捷译本?马上给分 文件操作问题,急!!!马上给分 java 新手上路 哪位大侠做过在java中调用服务器端的可执行应用程序? java线程的问题 如何在jtable的tableheader上面添加复选框 问一个关于JTable的问题
1.try {
double num = Double.parseDouble("123.3");
System.out.println("有效");
}
catch (NumberFormatException ex) {
System.out.println("无效");
}2.
java.util.regex.Pattern p=java.util.regex.Pattern.compile("^\\d+$|^\\d+\\.\\d+$");
java.util.regex.Matcher m=p.matcher("123");
if(m.matches()){
System.out.println("有效");
}else{
System.out.println("无效");
}
java.util.regex.Pattern p1=java.util.regex.Pattern.compile("^\\d+$");
java.util.regex.Pattern p2=java.util.regex.Pattern.compile("^\\d+\\.\\d+$");
java.util.regex.Matcher m1=p1.matcher(str);
java.util.regex.Matcher m2=p1.matcher(str);
if(m1.matches()){
System.out.println("字符串str全是数字!");
}else if(m2.matches()){
System.out.println("字符串str是个含有小数的数字!");
}else{
System.out.println("字符串str不是个有效数字!");
}
lip009(深蓝忧郁)
正解
lip009(深蓝忧郁)
正解