當前位置:首頁 » 編程語言 » java判斷字元是數字

java判斷字元是數字

發布時間: 2022-06-28 06:16:23

java 如何判斷一個字元串是數字

方法有很多,個人覺得使用異常捕捉非常方便,上代碼:

public static boolean isNumeric(String str){//捕獲NumberFormatException異常

if (str != null) str = str.trim();//" 3 "自動去空格再判斷

try{

Integer.parseInt(str);

return true;

}catch(NumberFormatException e){

System.out.println("異常:"" + str + ""不是數字/整數...");

return false;

}

}

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

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

或者用正則表達式

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

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

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

㈢ java裡面怎麼判斷字元是數字

importjava.util.Scanner;
importjava.util.TreeMap;
/**
*從鍵盤輸入16位長整數,編程統計每個數字出現的個數
*@authoryoung
*
*/
publicclassCharMapDemo{
//統計數字或者字元出現的次數
publicstaticTreeMap<Character,Integer>Pross(Stringstr){
char[]charArray=str.toCharArray();

TreeMap<Character,Integer>tm=newTreeMap<Character,Integer>();

for(intx=0;x<charArray.length;x++){
if(!tm.containsKey(charArray[x])){
tm.put(charArray[x],1);
}else{
intcount=tm.get(charArray[x])+1;
tm.put(charArray[x],count);
}
}
returntm;
}

publicstaticvoidmain(String[]args){
Scannersc=newScanner(System.in);
// System.out.println("請輸入一個長整數:");
// inttemp=sc.nextInt();
// Stringstr=String.valueOf(temp);
// TreeMap<Character,Integer>tm=Pross(str);
// System.out.println(tm);

System.out.println("請輸入一個字元串:");
Stringstr=sc.nextLine();
TreeMap<Character,Integer>tm=Pross(str);
System.out.println(tm);
}
}

以上是完整代碼。

㈣ 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判斷string是不是數字

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

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

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

㈥ java中怎麼判斷一個字元串是否為數字

可以使用Character類的isDigit逐位判斷。但是這樣無法判斷小數、負數。
也可以直接使用正則:
Pattern
pattern
=
Pattern.compile("-?[0-9]+\\.?[0-9]*");
String
str
=
"-55.5555";
Matcher
isNum
=
pattern.matcher(str);
if(
isNum.matches()
){
System.out.println("數字");
}

㈦ java如何做到判斷一個字元串是否是數字。

如果只是判斷,可與用integer.parseint(string)如果是數字,就沒有異常,如果有異常,就不是數字
或者用正則表達式
return
string.matches("\\d+\\.?\\d*"));

這個語句就是用來判斷的
\\d+表示一個或者多個數字
\\.?
表示一個或這沒有小數點
\\d
*
表示0個或者多個數字

㈧ Java中判斷字元串是否為數字的幾種方法

1.使用Character.isDigit(char)判斷
char num[] = str.toCharArray();//把字元串轉換為字元數組
StringBuffer title = new StringBuffer();//使用StringBuffer類,把非數字放到title中
StringBuffer hire = new StringBuffer();//把數字放到hire中
for (int i = 0; i < num.length; i++) {
// 判斷輸入的數字是否為數字還是字元
if (Character.isDigit(num[i])) {把字元串轉換為字元,再調用Character.isDigit(char)方法判斷是否是數字,是返回True,否則False
hire.append(num[i]);// 如果輸入的是數字,把它賦給hire} else {title.append(num[i]);// 如果輸入的是字元,把它賦給title}}}
2.使用類型轉換判斷try {String str="123abc";
int num=Integer.valueOf(str);//把字元串強制轉換為數字
return true;//如果是數字,返回True
} catch (Exception e) {
return false;//如果拋出異常,返回False}
3.使用正則表達式判斷
String str = "";
boolean isNum = str.matches("[0-9]+");
//+表示1個或多個(如"3"或"225"),*表示0個或多個([0-9]*)(如""或"1"或"22"),?表示0個或1個([0-9]?)(如""或"7")
ps:這個方法只能用於判斷是否是正整數

㈨ java中判斷字元串是否為數字的方法的幾種方法

可以使用正則表達式+中文字元編碼區間驗證一個字元串中是否包含漢字
代碼如下:
public
static
void
main(string[]
args)
{
int
count
=
0;
string
regex
=
"[\\u4e00-\\u9fa5]";
//system.out.println(regex);
string
str
=
"中文fdas
";
//system.out.println(str);
pattern
p
=
pattern.compile(regex);
matcher
m
=
p.matcher(str);
while
(m.find())
{
for
(int
i
=
0;
i
<=
m.groupcount();
i++)
{
count
=
count
+
1;
}
}
system.out.println("共有
"
+
count
+
"個
");
}

㈩ java中 怎麼判斷一個字元是數字

//這個你可以用正則表達式來篩選,其實你明白其中道理就可以了打個比方!
//chara='0';
//charb=0;
這個二個是不一樣的,怎麼來比較這個不一樣,可以用個偷懶的辦法,都加0就可以實現了!
a+0=48;
b+0=0;
所以可以結論出,char類型:'0','1','2'....'9';
可以用(48--57)這個范圍來判斷他們是數字字元還是,其他字元!
而chara=0,b=1,c=2;他們加0還是自身,就可以用(0---9)這個范圍來判斷!
其他其實a---z或者A--Z都有自己的范圍,我就不在例舉了,道理其實很簡單!
我用一個簡單的判斷,遍歷一個數組,你看看吧!
publicclass字元是數
{
publicstaticvoidmain(String[]args)
{
System.out.println(" ==========java中怎麼判斷一個字元是數字!========== ");
init();
}//初始化!
privatestaticvoidinit()
{
char[]arr={'0','9','a',0,'b',9,'W'};
for(inti=0;i<arr.length;i++)
{
if((arr[i]+0>=48)&&(arr[i]+0<=57))

System.out.println("第"+(i+1)+"個是數字字元");

elseif((arr[i]+0>=0)&&(arr[i]+0)<=9)

System.out.println("第"+(i+1)+"個是純數字!");
else
System.out.println("第"+(i+1)+"個是其他字元");
}

}
}

熱點內容
安卓哪個國家免費用 發布:2025-02-04 22:39:54 瀏覽:59
電腦配置低但想玩小偷模擬器怎麼辦 發布:2025-02-04 22:39:03 瀏覽:233
最快腳本語言 發布:2025-02-04 22:27:23 瀏覽:526
安卓的人臉識別在哪裡 發布:2025-02-04 22:16:45 瀏覽:674
悠然伺服器的ip是什麼 發布:2025-02-04 22:10:17 瀏覽:65
3des源碼 發布:2025-02-04 22:09:16 瀏覽:809
如何備份資料庫表 發布:2025-02-04 22:09:07 瀏覽:294
如何刪除下載的鬧鍾鈴聲安卓 發布:2025-02-04 22:03:35 瀏覽:658
死神腳本 發布:2025-02-04 21:57:03 瀏覽:168
phpposthtml 發布:2025-02-04 21:37:46 瀏覽:89