Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中解析通过将字符串拆分为Int值获得的数据?_Java_Arrays_Parseint - Fatal编程技术网

如何在Java中解析通过将字符串拆分为Int值获得的数据?

如何在Java中解析通过将字符串拆分为Int值获得的数据?,java,arrays,parseint,Java,Arrays,Parseint,我拆分了一个字符串,它是用户输入的密码,只返回数字。我需要确保他们输入的数字不是特定的数字。但首先,我需要将返回的数据转换成int,以便进行比较 public static boolean checkPassword(String password){ String digitsRegrex = "[a-zA-Z]+"; int upPass; String [] splitPass = password.split("[a-zA-Z]+");

我拆分了一个字符串,它是用户输入的密码,只返回数字。我需要确保他们输入的数字不是特定的数字。但首先,我需要将返回的数据转换成int,以便进行比较

public static boolean checkPassword(String password){
      String digitsRegrex = "[a-zA-Z]+";
      int upPass;

      String [] splitPass = password.split("[a-zA-Z]+");
      for(String pass : splitPass){
         try{
            upPass = Integer.parseInt(pass);
         }
         catch (NumberFormatException e){
            upPass = 0;
         }       
         System.out.println(upPass);
      }  
      return true;   
  }

当我运行该程序时,我会在catch中返回0(以及字符串密码中的数字),因此我猜尝试不起作用?

在代码中,当遇到不包含任何数字的子字符串时,可以将
upPass
设置为0。这些是空字符串。当密码不是以数字开头时会发生这种情况

您应该忽略它,因为您只需要数字

示例:
abcd12356zz33
-当您使用正则表达式
[a-zA-Z]+
进行拆分时,您会得到
“123456”
,以及
“33”
。当您尝试将第一个空字符串转换为数字时,会出现
NumberFormatException

for(String pass : splitPass){
    if (!pass.isEmpty()) {
        try {
            upPass = Integer.parseInt(pass);
            System.out.println(upPass);
            //Validate upPass for the combination of numbers
        } catch (NumberFormatException e) {
            throw e;
        }
    }
}

印刷品

12356
33

pass.matches(“[0-9]+”)和您现在使用的isEmpty之间有什么区别?是因为我已经把它分开了吗?我已经把那个部分去掉了。现在,我正在检查是否为空,因为原始正则表达式(
[A-Za-z]+
)已经确保不会有任何字母。
12356
33