判断输入的是否为数字
2018-08-18 本文已影响0人
神坛下的我
StringUtil.java
public class StringUtil {
public StringUtil(){}
private String numStr;//要判断的字符串
private boolean number;//判断结果
private String cue;//提示信息
public String getNumStr() {
return numStr;
}
public void setNumStr(String numStr) {
numStr=numStr.replaceAll(" ", "");
this.numStr = numStr;
}
public boolean isNumber() {
char cArr[]=numStr.toCharArray();
StringBuffer sb=new StringBuffer("");
for(int i=0;i<cArr.length;i++){
int ascii = (int)cArr[i];
if (ascii>=48&&ascii<=57) {
sb.append(cArr[i]);
}
}
if (sb.length()==cArr.length) {
number=true;
this.setCue("您输入的是数字!");
}else {
number=false;
this.setCue("您输入的不是数字!");
}
return number;
}
public void setNumber(boolean number) {
this.number = number;
}
public String getCue() {
return cue;
}
public void setCue(String cue) {
this.cue = cue;
}
}
index.jsp
<body>
<form action="result.jsp" method="post">
<table>
<tr>
<td align="right">请输入工资收入:</td>
<td><input type="text" name="numstr" />元</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="检查"/></td>
</tr>
</table>
</form>
</body>
result.jsp
<body>
<%
request.setCharacterEncoding("utf-8");
String numStr= request.getParameter("numstr");
%>
<jsp:useBean id="strBean" class="com.count.StringUtil"></jsp:useBean>
<jsp:setProperty property="numStr" name="strBean" value="<%=numStr %>"/>
<table>
<tr>
<td>输入的是:</td>
<td align="left">
<jsp:getProperty property="numStr" name="strBean"/>
</td>
</tr>
<tr>
<td>是否为数字:</td>
<td align="center">
<jsp:getProperty property="number" name="strBean"/>
</td>
</tr>
<tr>
<td>提示信息:</td>
<td align="right">
<jsp:getProperty property="cue" name="strBean"/>
</td>
</tr>
</table>
</body>
8.PNG
9.PNG