写一段程序辨别一个字符串是否是数字?
来源:百度知道 编辑:UC知道 时间:2024/06/26 00:47:37
java:
import java.text.*;
class DHCheck{
public DHCheck(String s){
code=s;
pos=code.indexOf("-");
s_num=code.substring(0,pos)+code.substring(pos+1);
s_qh=code.substring(0,pos);
}
public boolean check(){
if(!(pos==3||pos==4)) return false;
if(code.substring(pos+1).length()<7||code.substring(pos+1).length()>8) return false;
/* 另一种方式:辨别字符串是否是数字!!!
try{
Number n = (new DecimalFormat()).parse(s_num);
System.out.println(n);
}catch(ParseException e){
return false;
}
return true;
*/
for(int i=0;i<s_num.length();i++){
if(s_num.charAt(i)>'9'||s_num.charAt(i)<'0')
return false;
}
return true;
}
public String getQH(){
return s_qh;
}
private String code;
private String s_num;
private String s_qh;
private int pos;
}
一般常用