内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

java怎么判斷變量是否為數(shù)字-創(chuàng)新互聯(lián)

java怎么判斷變量是否為數(shù)字?針對(duì)這個(gè)問(wèn)題,這篇文章給出了相對(duì)應(yīng)的分析和解答,希望能幫助更多想解決這個(gè)問(wèn)題的朋友找到更加簡(jiǎn)單易行的辦法。

為沽源等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及沽源網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、沽源網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

1、用JAVA自帶的函數(shù)

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

2、用正則表達(dá)式

首先要import java.util.regex.Pattern 和 java.util.regex.Matcher

public boolean isNumeric(String str){
   Pattern pattern = Pattern.compile("[0-9]*");
   Matcher isNum = pattern.matcher(str);
   if( !isNum.matches() ){
       return false;
   }
   return true;
}

3、使用org.apache.commons.lang

org.apache.commons.lang.StringUtils;
boolean isNunicodeDigits=StringUtils.isNumeric("aaa123456789");
http://jakarta.apache.org/commons/lang/api-release/index.html下面的解釋:
isNumeric
public static boolean isNumeric(String str)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.
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
Parameters:
str - the String to check, may be null
Returns:
true if only contains digits, and is non-null

上面三種方式中,第二種方式比較靈活。

第一、三種方式只能校驗(yàn)不含負(fù)號(hào)“-”的數(shù)字,即輸入一個(gè)負(fù)數(shù)-199,輸出結(jié)果將是false;

而第二方式則可以通過(guò)修改正則表達(dá)式實(shí)現(xiàn)校驗(yàn)負(fù)數(shù),將正則表達(dá)式修改為“^-?[0-9]+”即可,修改為“-?[0-9]+.?[0-9]+”即可匹配所有數(shù)字。

如果我輸入的是"a“,它能識(shí)別出來(lái)這個(gè)不是數(shù)字,該怎么寫?

import java.io.* ;
import java.util.* ;
public class Test{
public static void main(String [] args) throws Exception{
System.out.println("請(qǐng)輸入數(shù)字:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   String line=br.readLine();
    if(line.matches("\\d+"))     //正則表達(dá)式 詳細(xì)見(jiàn)java.util.regex 類 Pattern
   System.out.println("數(shù)字");
   else
   System.out.println("非數(shù)字");
}
}

("\d")是數(shù)字0-9 ("\\d+")是什么意思?

正則表達(dá)式用兩個(gè)斜杠表示一個(gè)斜杠,后面跟著一個(gè)加號(hào)表示出現(xiàn)一次或多次,完整的意思就是整個(gè)字符串中僅包含一個(gè)或多個(gè)數(shù)字。

4、判斷ASCII碼值

 public static boolean isNumeric0(String str){
  for(int i=str.length();--i>=0;){
   int chr=str.charAt(i);
   if(chr<48 || chr>57)
    return false;
  }
  return true;
 }

5、逐個(gè)判斷str中的字符是否是0-9

 public static boolean isNumeric3(String str){
  final String number = "0123456789";
  for(int i = 0;i
            if(number.indexOf(str.charAt(i)) == -1){  
             return false;  
            }  
  }  
  return true;
 }

6、捕獲NumberFormatException異常

 public static boolean isNumeric00(String str){
  try{
   Integer.parseInt(str);
   return true;
  }catch(NumberFormatException e){
   System.out.println("異常:\"" + str + "\"不是數(shù)字/整數(shù)...");
   return false;
  }
 }

ps:不提倡使用方法6,原因如下:

1、NumberFormatException是用來(lái)處理異常的,最好不要用來(lái)控制流程的。

2、雖然捕捉一次異常很容易,但是創(chuàng)建一次異常會(huì)消耗很多的系統(tǒng)資源,因?yàn)樗o整個(gè)結(jié)構(gòu)作一個(gè)快照。

看一下JDK源碼:

 public static long parseLong(String s,int radix)  
         throws NumberFormatException  
 {  
    if(s == null){  
       throw   new   NumberFormatException("null");  
    }  
    if(radix < Character.MIN_RADIX){  
           throw new NumberFormatException("radix " + radix +
           " less than Character.MIN_RADIX");  
    }  
    if(radix > Character.MAX_RADIX){  
           throw new NumberFormatException("radix " + radix +
           " greater than Character.MAX_RADIX");  
    }  
    long result = 0;  
    boolean negative = false;
    int i = 0,max = s.length();  
    long limit;  
    long multmin;  
    int digit;
    if(max > 0){  
     if(s.charAt(0) == '-'){  
      negative = true;  
      limit = Long.MIN_VALUE;
      i++;
     }else{
      limit = -Long.MAX_VALUE;
     }  
     multmin = limit / radix;
     if(i < max){  
      digit = Character.digit(s.charAt(i++),radix);  
      if(digit < 0){
            throw new NumberFormatException(s);  
      }else{  
            result = -digit;
      }  
     }  
     while(i < max){  
      // Accumulating negatively avoids surprises near MAX_VALUE
      digit = Character.digit(s.charAt(i++),radix);  
      if(digit < 0){  
       throw new NumberFormatException(s);  
      }  
      if(result < multmin){  
       throw new NumberFormatException(s);  
      }  
      result *= radix;  
      if(result < limit + digit){  
       throw new NumberFormatException(s);  
      }  
      result -= digit;  
    }  
    }else{  
     throw   new   NumberFormatException(s);  
    }  
    if(negative){  
     if(i > 1){  
      return result;
     }else{  
      throw new NumberFormatException(s);  
     }  
    }else{  
     return   -result;  
    }  
 }

可以看出來(lái)jdk里也是一個(gè)字符一個(gè)字符的判斷,如果有一個(gè)不是數(shù)字就拋出NumberFormatException,所以還不如這個(gè)工作由我們自己來(lái)做,還省得再拋出一次異常。

關(guān)于java判斷變量是否為數(shù)字的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

文章名稱:java怎么判斷變量是否為數(shù)字-創(chuàng)新互聯(lián)
地址分享:http://www.rwnh.cn/article40/pjhho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、搜索引擎優(yōu)化標(biāo)簽優(yōu)化、App設(shè)計(jì)、網(wǎng)站內(nèi)鏈網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管
海阳市| 柘荣县| 星座| 宝兴县| 鹤庆县| 额济纳旗| 新巴尔虎右旗| 棋牌| 定日县| 威宁| 蛟河市| 宁陕县| 崇信县| 临安市| 乐东| 丰县| 开封县| 兴山县| 江津市| 襄垣县| 囊谦县| 浠水县| 伊宁市| 罗定市| 登封市| 汉源县| 汉源县| 应用必备| 福清市| 盐池县| 广河县| 和平区| 南雄市| 德惠市| 大石桥市| 公安县| 富裕县| 叶城县| 安丘市| 临沂市| 共和县|