Topic: !新手求解!(求 isNumberic)

  Print this page

1.!新手求解!(求 isNumberic) Copy to clipboard
Posted by: sealsealseal
Posted on: 2006-03-22 22:30

public class HH {
static boolean isNumberic(String num){
插入代碼
}
public static void main(String[] args) {
System.out.println(isNumberic("123"));
System.out.println(isNumberic("-123.23"));
System.out.println(isNumberic("0x123"));
System.out.println(isNumberic("12acb3"));
System.out.println(isNumberic("-aa123"));
}
输出:
true
true
false
false
false

2.Re:!新手求解! [Re: sealsealseal] Copy to clipboard
Posted by: zcjl
Posted on: 2006-03-23 00:30

没找到现成的isNumeric API
下面的代码纯粹是为了应付题目 ^_^
public static void main(String[] args) {
System.out.println(isNumberic("123"));
System.out.println(isNumberic("-123.23"));
System.out.println(isNumberic("0x123"));
System.out.println(isNumberic("12acb3"));
System.out.println(isNumberic("-aa123"));
}

public static boolean isNumberic(String num) {
try {
Double.parseDouble(num);
return true;
} catch (NumberFormatException e) {
return false;
}
}

3.Re:!新手求解!(求 isNumberic) [Re: sealsealseal] Copy to clipboard
Posted by: sealsealseal
Posted on: 2006-03-23 07:48

謝謝~

4.Re:!新手求解!(求 isNumberic) [Re: sealsealseal] Copy to clipboard
Posted by: suntao19830709
Posted on: 2006-03-23 17:41

这是apache的StringUtils的原码。

public static boolean isNumeric(String str) {
if (str == null) {
return false;
}
int sz = str.length();
for (int i = 0; i < sz; i++) {
if (Character.isDigit(str.charAt(i )) == false) {
return false;
}
}
return true;
}

5.Re:!新手求解!(求 isNumberic) [Re: sealsealseal] Copy to clipboard
Posted by: suntao19830709
Posted on: 2006-03-23 17:47

apache给出的注释

* StringUtils.isNumeric(null) = false
* StringUtils.isNumeric("") = true
* StringUtils.isNumeric(" ") = false
* StringUtils.isNumeric("123") = true
* StringUtils.isNumeric("12 3") = false
* StringUtils.isNumeric("ab2c") = false
* StringUtils.isNumeric("12-3") = false
* StringUtils.isNumeric("12.3") = false

6.Re:!新手求解!(求 isNumberic) [Re: sealsealseal] Copy to clipboard
Posted by: why
Posted on: 2006-03-23 18:10

http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/StringUtils.html#isNumeric(java.lang.String)
Checks if the String contains only unicode digits.
A decimal point is not a unicode digit and returns false.

null will return false. An empty String ("") will return true.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923