Find if an input String is valid - the criteria is String
lengthgreater than 5 and characters must bea-z, A-Z or 0-9.Valid strings:
aA12Z22,qwerty,ABCDEF0Not valid:
13A,12CrW#,@**=_+
I am trying to get it right using Java 8 streams. I have a solution here. The question - is there a better way of doing this (using streams)?
boolean valid = Stream.of(string)
.flatMapToInt(s -> {
if (s.length() < 5) return IntStream.of(42); // 42 is a '*' char
else return s.chars();
})
.mapToObj(i -> (char) i)
.allMatch(c -> Character.isLetterOrDigit(c));
System.out.println(valid);
string.matches("[A-Za-z0-9]{5,}")? Streams seem unnecessary, hard and inefficient.