The task is to check using Stream if such type of string "AZ6BYW59UO6CR8BNT7NM 284130" satisfies these conditions:
- 20 uppercase alphabetical chars or digits before the space, the number of digits is 6 strictly
- 6 digits after the space
Here is what I've done so far:
public static boolean validateCode(String input)
{
String[] words = input.split("\\s+");
ArrayList<String> wordList = new ArrayList<String>(Arrays.asList(words));
Stream<Character> left = wordList.get(0).chars().mapToObj(ch -> (char)ch);
Stream<Character> right = wordList.get(1).chars().mapToObj(ch -> (char)ch);
boolean leftIs20 = left
.collect(Collectors.counting()).equals(20L);
boolean leftisAlfaDigit = left
.allMatch(x -> (Character.isDigit(x) || Character.isUpperCase(x)));
boolean leftis6Digits = left.filter(x -> Character.isDigit(x)).equals(6L);
boolean rightAreDigits = right.allMatch(x ->Character.isDigit(x));
boolean rightAre6Digits = right.collect(Collectors.counting()).equals(6L);
return leftIs20 && leftisAlfaDigit && leftis6Digits && rightAreDigits && rightAre6Digits;
}
But as a stream cannot be reused, it is wrong, but I have no idea how to overcome the issue.