當前位置:首頁 » 編程語言 » java數字校驗

java數字校驗

發布時間: 2022-07-22 05:51:41

java如何在輸入框中判斷是否輸入的是數字

這里是Java後台驗證。

publicstaticvoidmain(String[]args){
//target是從文本框傳來的數據。這里進行模擬
Stringtarget="22221";
//如果為true,則是數字,否則不是。
System.out.println(isNumeric(target));
}

/**
*正則表達式:判斷是否數字
*@paramstr
*@return
*/
publicstaticbooleanisNumeric(Stringstr){
Patternpattern=Pattern.compile("[0-9]*");
returnpattern.matcher(str).matches();
}

也可以直接在前台驗證,用javascript驗證。同樣用正則表達式。

<scripttype="text/javascript">
functionvalidate(){
varreg=newRegExp("^[0-9]*$");
varobj=document.getElementById("name");
if(!reg.test(obj.value)){
alert("請輸入數字!");
}
if(!/^[0-9]*$/.test(obj.value)){
alert("請輸入數字!");
}
}
</script>

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

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

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

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

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

Ⅲ java數字位數檢驗

1樓的:如果我輸入 1.2 結果會是什麼樣子呢???

顯然是有BUG的,不正確

import java.util.Scanner;

public class JudgeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//輸入一個數
System.out.print("Please Input a number:");
String number = sc.nextLine();
if (number.length() == 1 && number.matches("^\\d$")) {//長度為1,且是數字
number = "0000" + number;
System.out.print(number);
} else {
System.out.print("不是一位數!");
}
}
}

上面是正確的

祝你好運!

Ⅳ java中怎麼判斷數字

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

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

Ⅳ 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驗證是否是數字

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中驗證字元串是不是數字的四種方法

判斷字元串是不是數字,大家可能會用一些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判斷字元串是不是阿拉伯數字的三種方法

1用JAVA自帶的函數
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(i);
if(chr<48 || chr>57)
return false;
}
return true;
}

熱點內容
甘肅隴南dns伺服器地址雲主機 發布:2025-03-05 00:43:52 瀏覽:384
一頓壓縮機 發布:2025-03-05 00:41:51 瀏覽:407
php二維數組拆分 發布:2025-03-05 00:33:24 瀏覽:198
游猴腳本 發布:2025-03-05 00:33:20 瀏覽:516
微信存儲的文檔 發布:2025-03-05 00:31:42 瀏覽:230
c語言中基本的數據類型包括 發布:2025-03-05 00:30:55 瀏覽:709
安卓小組件哪個好用 發布:2025-03-05 00:30:20 瀏覽:341
安卓如何下載錄屏精靈 發布:2025-03-05 00:15:22 瀏覽:486
攻城掠地怎麼開伺服器 發布:2025-03-05 00:11:31 瀏覽:483
怎麼看漢蘭達什麼配置 發布:2025-03-05 00:08:29 瀏覽:283