Bleusilences
Bleusilences

Reputation: 1

Trying to change part of a class Java

I am trying to change part of a class (Character) so I can change the end as .isDigit for example. Like I could change type tester to whaterver I want but I have no idea how I could do that.

 public static boolean testText (String password, int length,typeTester) {
    boolean test = false;
    for (int count = 0; count <= (length-1); count++){
      char resultConverted = password.charAt(count);
      if (Character.typeTest(resultConverted))
        test = true;
    }
    return test; 

  }

edit: so there is now way to do better then just do something like this to analyse a word:

  public static boolean testUpperCase (String password, int length) {
    boolean upperCase = false;
    for (int count = 0; count <= (length-1); count++){
      char resultConverted = password.charAt(count);
      if (Character.isUpperCase(resultConverted))
        upperCase = true;
    }

    return upperCase;
  }
  public static boolean testLowerCase (String word, int length) {
    boolean lowerCase = false;
    for (int count = 0; count <= (length-1); count++){
      char resultConverted = word.charAt(count);
      if (Character.isLowerCase(resultConverted))
        lowerCase = true;
    }
    return lowerCase;

  }

It's just a question do find a more elegant alternative to what I have done so far.

Upvotes: 0

Views: 79

Answers (2)

Tobias
Tobias

Reputation: 7771

Here is a more elegant variant of testUpperCase:

boolean testUpperCase(String password) {
  for(char c : password.toCharArray()) {
    if(Character.isUpperCase(c)) {
      return true;
    }
  }
  return false;
}

Upvotes: 1

David SN
David SN

Reputation: 3519

If the String is not equal to the String converted to lowercase means that there should be some uppercase characters.

/* Test if there are any uppercase character in the string */
boolean testUpperCase(String password) {
    return !password.equals(password.toLowerCase());
}

And the same to test if the String has lowercase characters:

/* Test if there are any lowercase character in the string */
boolean testLowerCase(String password) {
    return !password.equals(password.toUpperCase());
}

Upvotes: 0

Related Questions