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

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://www.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优选在线观看精品| 日韩欧美中文字幕综合网| 亚洲一区二区三区视频在线观看| 亚洲国产精品一区二区成人| 精精国产xxxx视频在线不卡| 99久久伊人精品综合观看| 国产精品xxxx国产精品| 国产自偷一区二区三区| 欧美精品三级不卡在线| 国产av一区最新精品麻豆| 熟女自拍偷拍视频播放| 日韩欧美一区二区三区不卡在线| 高清av网站大全网站| av色狠狠一区二区三区| 欧美丝袜熟女日韩亚洲| 精品人妻二区中文字幕| 成年自拍视频在线观看| 国产精品久久久在线视频| 亚洲av资源一区二区| 日韩中文字幕免费一区二区| 老熟妇仑乱换频一区二区| 麻豆人妻少妇精品毛片| 伊人激情久久综合中文字幕| 巨乳人妻一区二区三区| 日本在线一区二区三区免费视频| 在线麻豆国产传媒免费| 日本性电影一区二区| 91国语对白在线观看| 日本高清三级精品一区二区| 九九热超在线视频精品| 精品国产三级a在线观看网站| 欧美一区二区三区中文字幕| 国产一区二区三区日本精品|