欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

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

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

為沽源等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及沽源網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站設(shè)計制作、成都網(wǎng)站制作、沽源網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(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ù)號“-”的數(shù)字,即輸入一個負(fù)數(shù)-199,輸出結(jié)果將是false;

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

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

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

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

正則表達(dá)式用兩個斜杠表示一個斜杠,后面跟著一個加號表示出現(xiàn)一次或多次,完整的意思就是整個字符串中僅包含一個或多個數(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、逐個判斷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是用來處理異常的,最好不要用來控制流程的。

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

看一下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;  
    }  
 }

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

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

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

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

廣告

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

綿陽服務(wù)器托管
精华国产一区二区三区| 日韩欧美一区二区三区不卡在线| 91欧美日韩中在线视频| 女同av免费观看网站| 91亚洲蜜臀精品国产| 国产中文字幕有码视频| 韩国三级网站在线观看视频| 国产偷人伦激情在线观看| 三级久久三级久久三级| 国产粉嫩一区二区三区在线观看| 中文字幕乱码亚洲中文在线| 91欧美日韩中在线视频| 亚洲精品最新地址久久久| 午夜在线精品福利视频| 欧美另类亚洲综合久青草| 亚洲另类欧美日韩中文字幕| 婷婷色综合一区二区三区| 欧美一区二区日本国产激情| 五月天丁香婷婷一区二区| 精品国产自在现线拍手机| 午夜福利日本一区二区| 国产美女口爆吞精久久| 国产一区av麻豆免费观看| 一区二区三区高清av在线| 中文字幕国产精品综合一区| 日本成人一区二区三区在线| 亚洲av综合日韩精品久久| 日韩中文不卡人成在线视频| 黄色av在线免费观看| 国产白丝精品爽爽久久| 国产九色av在线一区尤物| 亚洲精品成人在线国产| 人妻少妇亚洲精品视频| 超碰免费在线公开97| 欧美一区二区三区有限公司| 夫妻过性生活视频播放| 国产免费看黄色的网站| 97久久久人妻精品一区 | 国产欧美日韩经典一区| 日本熟女视频免费观看| 欧美日韩另类激情免费|