/**
* 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;
}
Run Code Online (Sandbox Code Playgroud)
所以我基本上需要确保字符串只包含数字,如果它包含非数字,则该方法什么都不做。我遇到的问题是,如果一串数字中有一个随机字符,即“034343a45645”,它仍然会设置该方法。任何帮助表示感谢!