導航:首頁 > 編程語言 > 判斷是不是數字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相關的資料

熱點內容
sp源碼怎麼編輯修改 瀏覽:832
程序員男票怎麼樣 瀏覽:609
程序員招聘追求什麼 瀏覽:410
tracert命令的使用 瀏覽:983
金蜘蛛的指標源碼 瀏覽:880
探探資源網站源碼 瀏覽:942
php調用webserver 瀏覽:237
程序員配聽診器 瀏覽:76
程序員免費連wifi 瀏覽:494
王者榮耀今天怎麼伺服器更新了 瀏覽:153
單片機拼搭 瀏覽:153
程序員沒必要穿沖鋒衣 瀏覽:409
nova隱藏app怎麼用 瀏覽:680
單片機程序中ret 瀏覽:225
愛奇藝上海演算法團隊 瀏覽:140
程序員顏值高的人 瀏覽:364
西數硬體加密和閃迪軟體加密 瀏覽:718
聲控足球解壓黏土教程 瀏覽:641
linux下的嵌入式開發 瀏覽:175
電腦3d加速命令 瀏覽:107