请教高手,关于有意义的数值判断,在线等。 关于输入一个有意义的数值,而不是数字的判断,该怎么做?例如,人数01或001应该是错误的,金额00.1也是错误的,如何写个方法进行检查?只能用java在服务器端判定,不能用javascript。谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 就JSP被。数字判断的逻辑为什么是 001不对啊? 如果楼主坚持这样的逻辑就判断字符串呗。 转化成int或者float然后判断大小嘛 不用javascript,那用自定义标签了 我觉得如果你说的01,001,00.1如果能通过的话,那你设的一定是String类型的了,那就用equals()判断String val1="01";String val2="001";System.out.println(val1.equals(val2)); 或者你限制一下输入的长度,同样也可以在servlet里做,value1=request.getparamer("TEST");if(value1.length!=1){ String errors="There is some wrong in here."; Super.getInfo().setValue("errors",errors); return "JSP";} 用正则表达式是解决这一类问题的一个方法,例如你说的01和001的问题就可以说是数字首位必须是1-9之间的数字。public static boolean isNum(String str){ String formatEmail = "([1-9])[0-9]*$"; return str.matches(formatEmail); } public static void main(String[] args) throws Exception{ String sNum = "01"; System.out.println(isNum(sNum));}其他的你自己写就是了,这样灵活性比较大,什么email,指定小数精度之类的都可以做到 import java.util.regex.*;public static boolean isData(String str) { String regex = "^(-|\\+)?\\d+\\.?\\d*$"; //String regex="[0-9]*[.]?[0-9]*"; String regex1 = "^(-|\\+)?0+\\d+\\.?\\d*$"; Pattern ptn = Pattern.compile(regex); Matcher mat = ptn.matcher(str); boolean result = mat.matches(); System.out.println(">>> first result = " + result); if (1 > 2) { ptn = Pattern.compile(regex1); mat = ptn.matcher(str); System.out.println(">>> second result = " + mat.matches()); result = !mat.matches(); } mat = null; ptn = null; System.out.println(str + " is data ? " + result); return result; } //上面有点错 if (1 > 2)import java.util.regex.*;public static boolean isData(String str) { String regex = "^(-|\\+)?\\d+\\.?\\d*$"; //String regex="[0-9]*[.]?[0-9]*"; String regex1 = "^(-|\\+)?0+\\d+\\.?\\d*$"; Pattern ptn = Pattern.compile(regex); Matcher mat = ptn.matcher(str); boolean result = mat.matches(); System.out.println(">>> first result = " + result); if (result) { ptn = Pattern.compile(regex1); mat = ptn.matcher(str); System.out.println(">>> second result = " + mat.matches()); result = !mat.matches(); } mat = null; ptn = null; System.out.println(str + " is data ? " + result); return result; } 如何从一个不连续的整型数组中,取出空缺的数字重新组成一个数组? 求助一道简单的考试题的最佳答案,表达式怎么处理最有效率呢? 如何利用mediautil來將經緯度值寫入圖片的eixf中 [ 父类强制转换为子类,子类为何编译通过,运行失败? 关于一个简单的自定义的异常处理~~ 关于String类的小问题(50) 公司赶鸭子上架,我有些基础问题问问。 jar是什么玩意,请各位帮帮忙,我刚学JAVA 紧急求助;关于xls新旧版不兼容问题。 javascript的函数如何在HTML中使用求大神指教 this & super这两者的区别 这样的SQL怎么写
如果楼主坚持这样的逻辑就判断字符串呗。
value1=request.getparamer("TEST");
if(value1.length!=1)
{
String errors="There is some wrong in here.";
Super.getInfo().setValue("errors",errors);
return "JSP";
}
public static boolean isNum(String str){
String formatEmail = "([1-9])[0-9]*$";
return str.matches(formatEmail);
} public static void main(String[] args) throws Exception{
String sNum = "01";
System.out.println(isNum(sNum));
}其他的你自己写就是了,这样灵活性比较大,什么email,指定小数精度之类的都可以做到
public static boolean isData(String str)
{
String regex = "^(-|\\+)?\\d+\\.?\\d*$";
//String regex="[0-9]*[.]?[0-9]*";
String regex1 = "^(-|\\+)?0+\\d+\\.?\\d*$";
Pattern ptn = Pattern.compile(regex);
Matcher mat = ptn.matcher(str);
boolean result = mat.matches();
System.out.println(">>> first result = " + result);
if (1 > 2)
{
ptn = Pattern.compile(regex1);
mat = ptn.matcher(str);
System.out.println(">>> second result = " + mat.matches());
result = !mat.matches();
}
mat = null;
ptn = null;
System.out.println(str + " is data ? " + result);
return result;
}
{
String regex = "^(-|\\+)?\\d+\\.?\\d*$";
//String regex="[0-9]*[.]?[0-9]*";
String regex1 = "^(-|\\+)?0+\\d+\\.?\\d*$";
Pattern ptn = Pattern.compile(regex);
Matcher mat = ptn.matcher(str);
boolean result = mat.matches();
System.out.println(">>> first result = " + result);
if (result)
{
ptn = Pattern.compile(regex1);
mat = ptn.matcher(str);
System.out.println(">>> second result = " + mat.matches());
result = !mat.matches();
}
mat = null;
ptn = null;
System.out.println(str + " is data ? " + result);
return result;
}