// 判断是否数值的函数
public static boolean isNumeric(String inputstr)
{
boolean rv=true;
if(inputstr!=null)
{
byte[] bytearray=inputstr.getBytes();
int bytelength=bytearray.length;
int i=0;
while(i<bytelength&&rv)
{
if(bytearray[i]<48||bytearray[i]>57)
{
rv=false;
}
i=i+1;
}
}
else
{
rv=false;
}
return rv;
}
public static boolean isNumeric(String inputstr)
{
boolean rv=true;
if(inputstr!=null)
{
byte[] bytearray=inputstr.getBytes();
int bytelength=bytearray.length;
int i=0;
while(i<bytelength&&rv)
{
if(bytearray[i]<48||bytearray[i]>57)
{
rv=false;
}
i=i+1;
}
}
else
{
rv=false;
}
return rv;
}
int syear = Integer.parseInt(request.getAttribute("holid_year"));
boolean isNum = false;
try{
int n = Integer.parseInt(num)
} catch(Exception e) {
isNum = true;
}
if(isNum) {
System.out.println("num is not a Number");
} else {
System.out.println("num is a Number");
}
try{
Integer.parseInt(s);
return true;
}catch(Exception e){
return false;
}
}如果需要判断这个String是否double, 只要把上面的Integer改成Double就可以。然后在楼主代码??的地方用 isNumber(syear) 代替就可以
int year = 2003;
String syear = (String)request.getAttribute("holid_year");
if(syear != null && syear.length() >1 && ??){
year = Integer.parseInt(syear);
}
在你的代码中syear如果是空的话,你的syear.length()就会抛出异常。
public String intstr(String value)
{
String message=new String();
for(int i=0;i<value.length();i++)
{
if((value.charAt(i)>'9')||(value.charAt(i)<'0'))
{message="不是整形"; break;}
else
{message="是整形";}
}
return message;
}
你可以根据返回的的message判断是不是整形
程序应该这样写:
int year = 2003;
String syear = (String)request.getAttribute("holid_year");try{
if (syear != null && syear.length() > 1) {
year = Integer.parseInt(syear);
}
}catch(NumberFormatException e){
.....
}
public static void main(String[] args)
{
String input = args[0];
String ps="\\d+" ;
Pattern p = Pattern.compile(ps);
Matcher m = p.matcher(input);
if(m.matches()) {
System.out.println("ok");
}
else {
System.out.println("shit"); }
}}
String syear = (String)request.getAttribute("holid_year");
if(syear != null && syear.length()>1&&syear.compareTo("9999")<0&&syear.compareTo("0000")>0){
year = Integer.parseInt(syear);
}
小弟有礼了 fan