How about looping over the String
char by char and checking if it's a digit:
final List<Integer> values = new LinkedList<>();
final StringBuilder operator = new StringBuilder();
for (final char c : input.toCharArray()) {
if (Character.isDigit(c)) {
values.add(Integer.parseInt(Character.toString(c)));
} else if (!Character.isWhitespace(c)) {
operator.append(c);
}
}
Note: This will only work if the input is of the form "WdigitWdigitWoperator" where "W" is optional white space.
But I understand that that is the case from your question.
Another option is to use regex, this would look something like:
final Pattern p = Pattern.compile("^\\s*+(?<firstDigit>\\d)\\s*+(?<secondDigit>\\d)\\s*+(?<operator>\\w++)$");
final Matcher matcher = p.matcher(input);
if(matcher.matches()) {
System.out.println(matcher.group("firstDigit"));
System.out.println(matcher.group("secondDigit"));
System.out.println(matcher.group("operator"));
}
If this is a homework assignment (which I suspect it is) this may not be an acceptable solution however.