You can use String.codePoints
method to get a stream over int
values of characters of this string and count
quantity of them before first occurrence of non-lowercase and non-digit character. Your code might look something like this:
public static void main(String[] args) {
System.out.println(isLowerCaseOrDigits("EqwerJ")); // false
System.out.println(isLowerCaseOrDigits("as56re")); // true
System.out.println(isLowerCaseOrDigits("vb3451")); // true
System.out.println(isLowerCaseOrDigits("827136")); // true
System.out.println(isLowerCaseOrDigits("8271)&")); // false
}
private static boolean isLowerCaseOrDigits(String str) {
return str.codePoints()
.takeWhile(ch -> Character.isLowerCase(ch)
|| Character.isDigit(ch))
.count() == str.length();
}
Or without stream
, you can use String.toCharArray
method and iterate over the array of characters of this string. Return false
on the first occurrence of non-lowercase and non-digit character:
private static boolean isLowerCaseOrDigits(String str) {
for (char ch : str.toCharArray()) {
if (!Character.isLowerCase(ch) && !Character.isDigit(ch)) {
return false;
}
}
return true;
}