導航:首頁 > 編程語言 > java是否為數字

java是否為數字

發布時間:2021-10-24 21:45:23

java 判斷是否為數字

貌似Integer類里有個將字元串轉換為整數的方法,如果不能轉換就會拋異常,如果拋異常就說明不是數字了

② java中怎麼判斷數字

java中判斷一個字元是否為數字,可以通過Integer類的方法來判斷,如果拋出異常,專則不是數字屬,如下例子:

可以用異常來做校驗
/**
*判斷字元串是否是整數
*/
publicstaticbooleanisInteger(Stringvalue){
try{
Integer.parseInt(value);//判斷是否為數字
returntrue;
}catch(NumberFormatExceptione){//拋出異常
returnfalse;
}
}

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

使用Character.isDigit(char)判斷,類型轉換判斷,正則表達式判斷都是可以的,下面以正則判斷為例。

代碼如下:

Numeric(Stringstr)
{
Patternpattern=Pattern.compile("[0-9]*");////+表示1個或多個(如"3"或"225"),*表示0個或多個([0-9]*)(如""或"1"或"22"),?表示0個或1個([0-9]?)(如""或"7")
MatcherisNum=pattern.matcher(str);
if(!isNum.matches())//這里作判斷
{
returnfalse;
}
returntrue;//只有匹配了才會執行這里
}

④ java驗證是否是數字

publicbooleanisNumber(Stringstr)
{
java.util.regex.Patternpattern=java.util.regex.Pattern.compile("[0-9]*");
java.util.regex.Matchermatch=pattern.matcher(str);
if(match.matches()==false)
{
returnfalse;
}
else
{
returntrue;
}
}

⑤ java中如何判斷String中的內容是否為數字

服了樓上的了,
一樓的想法,
空間效率都不敢恭維。
回應二樓的,
不知大牛們是否都用這種想法用在實際程序中,
我倒想用正則表達式來做,
就是不知道像
12.
.23
之類的樓主認不認為是數字
[\d]+
or
[\d]*.[\d]*(具體寫法忘記了)

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

用正則表達式

public static boolean isNumericzidai(String str) {
Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+");
Matcher isNum = pattern.matcher(str); if (!isNum.matches()) { return false;
} return true;
}12345678

網上給出的最好的方法,可惜還是錯誤;首先正則表達式-?[0-9]+.?[0-9]+這里就錯誤
網上說:可匹配所有數字。
比如:

double aa = -19162431.1254;
String a = "-19162431.1254";
String b = "-19162431a1254";
String c = "中文";
System.out.println(isNumericzidai(Double.toString(aa)));
System.out.println(isNumericzidai(a));
System.out.println(isNumericzidai(b));
System.out.println(isNumericzidai(c));12345678

結果

falsetruetruefalse1234

正確的正則表達式是:-?[0-9]+\.?[0-9]*,點號.,是匹配任意字元,需要進行轉義。

⑦ java中如何判斷String中的內容是否為數字

這里提供3種方法:
判斷字元串是否為數字:
1分解法
public static boolean isNumeric(String str){
for (int i = str.length() ; --i>=0 ; ){
if (!Character.isDigit(str.charAt ( i ) ) ){
return false;
}
}
return true;
}

2>用正則表達式
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}

3>用ascii碼

public static boolean isNumeric(String str){
for(int i=str.length();--i>=0 {
int chr=str.charAt ;
if(chr<48 || chr>57)
return false;
}
return true;
}

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

以下是個公共類:
public class CharTest {
public CharTest() {
}
public static void main(String args[]) {
String s = "123abc";
for (int i=0;i<s.length();i++) {
char ch = s.charAt(i);
System.out.println(ch + " is digit? " + Character.isDigit(ch));
System.out.println(ch + " is digit? " + (ch >= '0' && ch <= '9'));
}
}
}
以下使用正則表達式來驗證
if(str==null || "".equals(str.trim())){
return true;
}
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str.trim());
if(isNum.matches()){
System.out.println("驗證為數字");
}else{
System.out.println("驗證不是數字");
}

⑨ java中判斷字元串是否為純數字

java中判斷字元串是否為純數字的方法如下:
方法一:Pattern語句
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Testone {
public static void main(String[ ] args){
String str="123456";
Pattern pattern = Pattern.compile("[0-9]{1,}");
Matcher matcher = pattern.matcher((CharSequence)str);
boolean result=matcher.matches();
if (result == true) {
System.out.println("該字元串是純數字");
}
else{
System.out.println("該字元串不是純數字");
}
}
}
方法二:正則表達式
<span style="white-space:pre"></span>
public static boolean isNumber(String str){
String reg = "^[0-9]+(.[0-9]+)?$";
return str.matches(reg);
}

閱讀全文

與java是否為數字相關的資料

熱點內容
四級丶四級電影﹥ 瀏覽:582
怎麼把cad的工具欄調出來 瀏覽:742
強奸了女僵屍的電影 瀏覽:15
能在線觀看最新網址 瀏覽:317
3d電影下載網站3d電影 瀏覽:261
華為手機如何把app弄成小窗口 瀏覽:589
flash的工具欄 瀏覽:106
古風sq片 瀏覽:705
如何挑選軟體編程培訓機構 瀏覽:873
巫師3112升級 瀏覽:163
zipjs怎麼用 瀏覽:619
手游天龍八部俠客升級 瀏覽:437
iphone5s蜂窩數據和3g有什麼區別 瀏覽:547
文件被病毒隱藏win10 瀏覽:710
主角重生到德國的小說 瀏覽:410
win10創意版本 瀏覽:436
韓國姜恩惠電影集合 瀏覽:436
無錫少兒編程哪裡好 瀏覽:779
電腦刪除一個文件就藍屏 瀏覽:95
淘寶商品數據包怎麼用 瀏覽:244

友情鏈接