導航:首頁 > 編程語言 > 判斷是不是數字Java

判斷是不是數字Java

發布時間:2022-12-31 19:04:56

java 判斷是否為數字

用正則判斷太麻煩,涉及到int、long、float、double等等

給你個通用版的

importjava.math.BigDecimal;

publicclass${
publicstaticvoidmain(String[]args){

System.out.println(isNum("1"));
System.out.println(isNum("1.1"));
System.out.println(isNum("1a"));
}

privatestaticbooleanisNum(Stringstr){
try{

newBigDecimal(str);
returntrue;
}catch(Exceptione){
returnfalse;
}
}
}

❷ java中判斷字元串是不是數字

調用string.toCharArray方法,把字元串轉化成字元數組。然後用for循環判斷其中每一個成員是不是數字就行了。 char[i] 大於等於0 小於等於9 之間的都是數字。

❸ java中怎麼判斷指定的數據是字元串是否是數字

java中判斷字元串是否為數字的方法:

1.用JAVA自帶的函數

public static boolean isNumeric(String str){for (int i = 0; i < str.length(); System.out.println(str.charAt(i));

if (!Character.isDigit(str.charAt(i))){return false;} }return true}

2.用正則表達式

首先要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; }

❹ Java怎樣判斷輸入是否為數字

你可以用try{}catch來處理,如果轉換的時候出錯了,那就肯定不是數字

❺ java判斷是否是數字

你可以用try{}catch(){}異常機制,把你傳入的字元串轉換成數字,如果發生異常就不是數字,沒有發生異常則是數字

❻ Java中怎樣判斷一個字元串是否是數字

用java的異常機制,不僅可以判斷是否是數字,還可以判斷整數或者小數:
public void checkInt(String bh){
try{
int num = Integer.parseInt(bh);//將輸入的內容轉換成int
System.out.println("是整數:"+num);//是整數
}catch (NumberFormatException e) {//轉換成int類型時失敗
try{
double d =Double.parseDouble(bh);//轉成double類型
System.out.println("是小數:"+d);//是小數
}catch (NumberFormatException e2) {//轉成double類型失敗
System.out.println("不是數字");
}
}
}

❼ java怎麼判斷一個字元串是否是數字

如果只是判斷,可與用integer.parseint(string)如果是數字,就沒有異常,如果有異常,就不是數字

或者用正則表達式

return
string.matches("\\d+\\.?\\d*"));

這個語句就是用來判斷的
\\d+表示一個或者多個數字

\\.?
表示一個或這沒有小數點
\\d
*
表示0個或者多個數字

❽ java中驗證字元串是不是數字的四種方法

判斷字元串是不是數字,大家可能會用一些java自帶的方法,也有可能用其他怪異的招式,比如判斷是不是整型數字,將字元串強制轉換成整型,不是數字的就會拋出錯誤,那麼就不是整型的了。但本文介紹的比較好的兩種方法:
1。java類庫自帶的方法:
public boolean isNum(String msg){
if(java.lang.Character.isDigit(msg.charAt(0))){
return true;}return false;}0202更新:發現以上方法寫得不夠到位,現在就改為下面的簡單說明了,至於具體的方法實現字元串判斷是否數字就不寫了。
java.lang.Character.isDigit(char ch) boolean
isDigit 只能作用於char,所以判斷字元串是否為數字,要一個一個拿出char進行判斷。
2。用正則表達式
首先要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;}02
3。用正則表達式

❾ Java中怎樣判斷一個字元串是否是數字

ava中判斷字元串是否為數字的方法:

1.用JAVA自帶的函數
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.用正則表達式
首先要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

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

第一、三種方式只能校驗不含負號「-」的數字,即輸入一個負數-199,輸出結果將是false;

而第二方式則可以通過修改正則表達式實現校驗負數,將正則表達式修改為「^-?[0-9]+」即可,修改為「-?[0-9]+.?[0-9]+」即可匹配所有數字。

閱讀全文

與判斷是不是數字Java相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:757
蘋果郵件無法連接伺服器地址 瀏覽:962
phpffmpeg轉碼 瀏覽:671
長沙好玩的解壓項目 瀏覽:142
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:736
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:302
PDF分析 瀏覽:484
h3c光纖全工半全工設置命令 瀏覽:143
公司法pdf下載 瀏覽:381
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:349
風翼app為什麼進不去了 瀏覽:778
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:150
伊克塞爾文檔怎麼進行加密 瀏覽:892
app轉賬是什麼 瀏覽:163