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

熱點內容
獸醫雲平台伺服器 瀏覽:295
php循環字母 瀏覽:636
盛達是什麼APP 瀏覽:196
android計算屏幕高度 瀏覽:651
自己做紅包解壓球 瀏覽:473
pdf很貴嗎 瀏覽:492
如何搶購騰訊雲伺服器 瀏覽:387
電子書app源碼 瀏覽:868
程序員在上班的時候 瀏覽:403
游戲壓縮文件夾怎麼刪除 瀏覽:420
百度新聞app是什麼 瀏覽:889
自動化對初級編程的影響 瀏覽:106
單片機綜合課程設計 瀏覽:602
程序員小嚴 瀏覽:813
如何下載歡樂走app 瀏覽:440
程序員節公司請美女 瀏覽:16
三本程序員好嗎 瀏覽:23
la78040場幅壓縮 瀏覽:902
MFC經典游戲編程 瀏覽:789
在線申請小額貸款源碼 瀏覽:336