简体   繁体   English

检查字符串是否仅包含数字,然后仅在不包含数字以外的字符时进行设置

[英]Checking if a string contains only digits and then setting it only if it doesn't contain characters other than digits

/**
 * Set the person's mobile phone number
 */
public void setMobile(String mobile) {
    for (int i = 0; i < mobile.length(); i++) {
if (!Character.isDigit(mobile.charAt(i))) {}
}
this.mobile = mobile;
}

So I basically need to make sure the string only contains digits, and if it contains non-digits for the method just to do nothing. 因此,我基本上需要确保该字符串仅包含数字,并且如果该字符串包含非数字,则该方法什么也不做。 The problem I have is that if there is a random character in a string of numbers ie "034343a45645" it will still set the method. 我的问题是,如果一串数字中有一个随机字符,即“ 034343a45645”,它将仍然设置该方法。 Any help is appreciated thanks! 任何帮助表示赞赏,谢谢!

You could use String.matches(String regex) : 您可以使用String.matches(String regex)

boolean onlyDigits = mobile.matches("[0-9]+");

With a for loop you can just break when a non-digits is found. 使用for循环,当发现非数字时,您可以中断。

boolean onlyDigits = true;
for (int i = 0; i < mobile.length(); i++) {
    if (!Character.isDigit(mobile.charAt(i))) {
        onlyDigits = false;
        break;
   }
}

Instead of breaking out of the loop, you could also just return. 除了打破循环,您还可以返回。 As it sounds like you don't want anything else to happen after. 听起来您不希望之后发生任何其他事情。 Thus eliminating the need for the onlyDigits variable to begin with. 这样就消除了对onlyDigits变量的需求。

Note that if mobile.length() == 0 then onlyDigits would still be true in relation to the above for loop. 请注意,如果mobile.length() == 0则相对于上述for循环, onlyDigits仍然为true So assuming onlyDigits should be false if mobile is an empty string, then you could initialize it as such: 因此,假设如果mobile是一个空字符串,则onlyDigits应该为false ,则可以这样初始化它:

boolean onlyDigits = !mobile.isEmpty()

After checking you can then assign it if onlyDigits is true . 检查后,如果onlyDigitstrue ,则可以分配它。

if (onlyDigits)
    this.mobile = mobile;

You have two problems: 您有两个问题:

First: you didn't exit the loop if the if condition is false 第一:如果条件为假,则您没有退出循环

Second: why using loop try implement tryParse like this 第二:为什么要使用循环尝试像这样实现tryParse

boolean tryParseInt(String value) {  
     try {  
         Integer.parseInt(value);  
         return true;  
      } catch (NumberFormatException e) {  
         return false;  
      }  
}

if(tryParseInt(mobile)){
  this.mobile = mobile;
}

add a boolean letterFound; 添加一个boolean letterFound;

then at your for loop 然后在你的for循环

whenever a letter is found use your else statement to set letterFound to true 每当找到字母时,请使用else语句将letterFound设置为true

then immediately stop the loop i=mobile.length() at your else statement 然后立即在您的else语句中停止循环i=mobile.length()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正则表达式检查字符串是否仅包含数字不起作用 - Regex check if a string contains only digits doesn't work 检查字符串是否仅包含数字且使用REGEX的数字是否相同? - Check if a String only contains digits and the digits are not the same using REGEX? 如何检查字符串是否仅包含 Java 中的数字 - How to check if a string contains only digits in Java 检查字符串是否仅包含允许的字符 - Checking if string contains only allowed characters 检查字符串是否仅包含某些字符 - Checking if a string only contains certain characters 如何检查字符串只包含数字和小数点? - How to check a string contains only digits and decimal points? 用于检查字符串是否仅包含数字和运算符(但没有 2 个连续运算符)的正则表达式 - Regex expression to check if a string only contains digits and operators (but no 2 consecutive operators) 如何检查字符串只包含数字和一个小数点? - How to check a string contains only digits and one occurrence of a decimal point? 来自仅包含数字的字符串的JAVA最大值 - JAVA Max Value from a String that contains only digits 设置一个值是否比检查存在性和仅设置不存在(例如在此Android / Java代码中)要快? - Is just setting a value faster than the checking of existance and only setting if doesn't exist - like in this Android / Java code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM