62,623
社区成员
发帖
与我相关
我的任务
分享public class Test {
public static void main(String[] args) {
System.out.println(isNumberic( "123.3"));
System.out.println(isNumberic( "-123.23"));
System.out.println(isNumberic( "-0x123"));
System.out.println(isNumberic( "12acb3"));
System.out.println(isNumberic( "-aa123"));
System.out.println(isNumberic( "+-+-017"));
System.out.println(+-+011);
}
public static boolean isNumberic(String str) {
return str.matches("([+-]?|(-\\+)*[-]?|(\\+-)*[+]?)(([1-9]\\d*(\\.\\d*)*)|(0[xX]\\p{XDigit}+)|(0[0-7]+))");
}
}
让系统给你做嘛不就行了
public static boolean isNumberic(String str){
int i =0;
float f = 0;
try{
if(str.indexOf(.) > 0){
f = Float.parseFloat(str);
}
if(str.indexOf(0x) > 0)){
i = Integer.parseInt(str,16);
}
if(str.charAt(0)==0&& str.charAt(1)!= x){
i = Integer.parseInt(str,8);
}
i = Integer.parseInt(str);
}catch(NumberNotFormatException e){
return false;
}
return true;
}