isDigits从方法名字就可以看得出来,是很简单地判断你给定的字符串是否完全由数字组成,而isNumber方法则行为复杂很多,Java number,可以是以0x开头的Hex,可以是带有类型修饰符的数字123L之类的,还可以是科学计数法表示的数字比如4E+10之类的。。类库的源码如下: << public static boolean isNumber(String str) { if ((str == null) || (str.length() == 0)) { return false; } char[] chars = str.toCharArray(); int sz = chars.length; boolean hasExp = false; boolean hasDecPoint = false; boolean allowSigns = false; boolean foundDigit = false; // deal with any possible sign up front int start = (chars[0] == '-') ? 1 : 0; if (sz > start + 1) { if (chars[start] == '0' && chars[start + 1] == 'x') { int i = start + 2; if (i == sz) { return false; // str == "0x" } // checking hex (it can't be anything else) for (; i < chars.length; i++) { if ((chars[i] < '0' || chars[i] > '9') && (chars[i] < 'a' || chars[i] > 'f') && (chars[i] < 'A' || chars[i] > 'F')) { return false; } } return true; } } sz--; // don't want to loop to the last char, check it afterwords // for type qualifiers int i = start; // loop to the next to last char or to the last char if we need another digit to // make a valid number (e.g. chars[0..5] = "1234E") while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) { if (chars[i] >= '0' && chars[i] <= '9') { foundDigit = true; allowSigns = false; } else if (chars[i] == '.') { if (hasDecPoint || hasExp) { // two decimal points or dec in exponent return false; } hasDecPoint = true; } else if (chars[i] == 'e' || chars[i] == 'E') { // we've already taken care of hex. if (hasExp) { // two E's return false; } if (!foundDigit) { return false; } hasExp = true; allowSigns = true; } else if (chars[i] == '+' || chars[i] == '-') { if (!allowSigns) { return false; } allowSigns = false; foundDigit = false; // we need a digit after the E } else { return false; } i++; } if (i < chars.length) { if (chars[i] >= '0' && chars[i] <= '9') { // no type qualifier, OK return true; } if (chars[i] == 'e' || chars[i] == 'E') { // can't have an E at the last byte return false; } if (!allowSigns && (chars[i] == 'd' || chars[i] == 'D' || chars[i] == 'f' || chars[i] == 'F')) { return foundDigit; } if (chars[i] == 'l' || chars[i] == 'L') { // not allowing L with an exponoent return foundDigit && !hasExp; } // last character is illegal return false; } // allowSigns is true iff the val ends in 'E' // found digit it to make sure weird stuff like '.' and '1E-' doesn't pass return !allowSigns && foundDigit; } >>
当不考虑科学计数法的时候,我的想法,不对的话请指正。 public static boolean isNumber(String str) { if(str!=null) str=str.trim(); if ((str == null) || (str.length() == 0)) { return false; } int number=0; //记录小数点出现的次数 if(str.charAt(0)!='-'||!Character.isDigit(charAt(0))) return false; for (int i = 1; i < str.length(); i++) { if (!Character.isDigit(str.charAt(i))&&str.charAt(i)!='.') { return false; else if(str.charAt(i)=='.') number++; if(number>1) return false; } } return true; }
这是我的方法,可以判断是否为整数 import java.io.*;public class Cin{ public static void main(String[] args) { String b = cinput(); if(b.equals("")) { System.out.println("What did you enter?"); } else { try { int d = Integer.parseInt(b); System.out.println("您所输入的数字是:" + b); } catch(NumberFormatException e) { System.out.println("You should type in number,try again"); } } } public static String cinput(){
String a = ""; try{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); a = in.readLine(); in.close(); } catch(IOException e){ System.out.println("error"); } return a; } }
用parseDouble,parseInt这样的方法就可以了吧? public int isNumber(String x){ try{ Double.parseDouble(x); }catch(Exception ex){ return 1; } return 0; }
<<
public static boolean isDigits(String str) {
if ((str == null) || (str.length() == 0)) {
return false;
}
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
>>
String str;
for(int i=0;i<str.length;i++){
String substring=str.subString(i,i+1);
if(("0123456789").indexof(substring)==-1){
return false;
}
}
给的方法只能判断试是不是整数 而且只能是正整数的时候才返回true;
如果能给出isNumber的源码也如此简单哪才更好呢!
<<
public static boolean isNumber(String str) {
if ((str == null) || (str.length() == 0)) {
return false;
}
char[] chars = str.toCharArray();
int sz = chars.length;
boolean hasExp = false;
boolean hasDecPoint = false;
boolean allowSigns = false;
boolean foundDigit = false;
// deal with any possible sign up front
int start = (chars[0] == '-') ? 1 : 0;
if (sz > start + 1) {
if (chars[start] == '0' && chars[start + 1] == 'x') {
int i = start + 2;
if (i == sz) {
return false; // str == "0x"
}
// checking hex (it can't be anything else)
for (; i < chars.length; i++) {
if ((chars[i] < '0' || chars[i] > '9')
&& (chars[i] < 'a' || chars[i] > 'f')
&& (chars[i] < 'A' || chars[i] > 'F')) {
return false;
}
}
return true;
}
}
sz--; // don't want to loop to the last char, check it afterwords
// for type qualifiers
int i = start;
// loop to the next to last char or to the last char if we need another digit to
// make a valid number (e.g. chars[0..5] = "1234E")
while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) {
if (chars[i] >= '0' && chars[i] <= '9') {
foundDigit = true;
allowSigns = false; } else if (chars[i] == '.') {
if (hasDecPoint || hasExp) {
// two decimal points or dec in exponent
return false;
}
hasDecPoint = true;
} else if (chars[i] == 'e' || chars[i] == 'E') {
// we've already taken care of hex.
if (hasExp) {
// two E's
return false;
}
if (!foundDigit) {
return false;
}
hasExp = true;
allowSigns = true;
} else if (chars[i] == '+' || chars[i] == '-') {
if (!allowSigns) {
return false;
}
allowSigns = false;
foundDigit = false; // we need a digit after the E
} else {
return false;
}
i++;
}
if (i < chars.length) {
if (chars[i] >= '0' && chars[i] <= '9') {
// no type qualifier, OK
return true;
}
if (chars[i] == 'e' || chars[i] == 'E') {
// can't have an E at the last byte
return false;
}
if (!allowSigns
&& (chars[i] == 'd'
|| chars[i] == 'D'
|| chars[i] == 'f'
|| chars[i] == 'F')) {
return foundDigit;
}
if (chars[i] == 'l'
|| chars[i] == 'L') {
// not allowing L with an exponoent
return foundDigit && !hasExp;
}
// last character is illegal
return false;
}
// allowSigns is true iff the val ends in 'E'
// found digit it to make sure weird stuff like '.' and '1E-' doesn't pass
return !allowSigns && foundDigit;
}
>>
public static boolean isNumber(String str) {
if(str!=null)
str=str.trim();
if ((str == null) || (str.length() == 0)) {
return false;
}
int number=0; //记录小数点出现的次数
if(str.charAt(0)!='-'||!Character.isDigit(charAt(0)))
return false;
for (int i = 1; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))&&str.charAt(i)!='.') {
return false;
else if(str.charAt(i)=='.')
number++;
if(number>1)
return false;
}
}
return true;
}
asserTrue(isNumber("12345"));
第一个测试样例就失败了
import java.io.*;public class Cin{ public static void main(String[] args)
{
String b = cinput();
if(b.equals(""))
{
System.out.println("What did you enter?");
}
else
{
try
{
int d = Integer.parseInt(b);
System.out.println("您所输入的数字是:" + b);
}
catch(NumberFormatException e)
{
System.out.println("You should type in number,try again");
}
}
}
public static String cinput(){
String a = "";
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
a = in.readLine();
in.close();
}
catch(IOException e){
System.out.println("error");
}
return a;
}
}
try{
Double.parseDouble(x);
}catch(Exception ex){
return 1;
}
return 0;
}
我说的是我刚才自己说的那个方法,老大!用java.lang.Double类的valueOf(String str)方法,然后catch它的NumberFormatException
>>娃哈哈~~
不过确实有点土,异常就是异常,不应该把异常用在正常的流程控制。
因此俺还是觉得使用jakarta-commons-lang类库来处理这个问题是最方便的。
{
boolean pointfirsttime = true; int i = 0;
if (s == null) {
return false;
}
if (s.charAt(0) == '-') {
i++;
}
while (i < s.length()) {
if (!Character.isDigit(s.charAt(i))) {
if ('.' == s.charAt(i) && pointfirsttime) {
pointfirsttime = false;
} else {
return false;
}
}
i++;
}
return true;
}尽量不要用异常控制流程,产生一个异常系统开销是很大的。