It is a bit confusing that you change val (the parameter).
Maybe use javaVal or patternVal (and maybe pattern instead of thePattern below).