Finding all uppercase letters of a string in java

前端 未结 10 1062
后悔当初
后悔当初 2021-01-12 14:28

So I\'m trying to find all the uppercase letters in a string put in by the user but I keep getting this runtime error:

Exception in thread \"main\" java.lan         


        
10条回答
  •  迷失自我
    2021-01-12 14:58

    Try this...

    Method:

    public int findUpperChar(String valitateStr) {
        for (int i = valitateStr.length() - 1; i >= 0; i--) {
            if (Character.isUpperCase(valitateStr.charAt(i))) {
                return i;
            }
        }
        return -1;
    }
    

    Usage:

    String passwordStr = password.getText().toString();
    
     .......
    
    int len = findUpperChar(passwordStr);
    
    if ( len != -1) {
    
          capitals exist.   
    
      } else {
    
          no capitals exist.            
    }
    

提交回复
热议问题