Use String#substring
to extract your digits, String#split
to get an array of digits and then loop over them (classic loop or stream API) to look up numeric values for the digit words. You can use a switch-case statement or a lookup map for the last step. Finally, sum and multiply your digits
final Map<String, Integer> digitmap = Map.ofEntries(
Map.entry("zero", 0),
Map.entry("one", 1),
Map.entry("two", 2),
…);
final String input = "…";
final String[] digitwords = input.substring("your identification code is ".length()).split(" ");
final List<Integer> digits = Arrays.stream(digitwords)
.map(digitmap::get)
.toList();
long result = 0;
for (int i = 0; i < digits.size(); ++i) {
result = result*10 + digits.get(i);
}
If you want a string and preserve leading zeros, then don't map "zero" -> 0
, but "zero" -> "0"
and then String.join
instead of sum + multiply or collect in a string builder:
final Map<String, String> digitmap = Map.ofEntries(
Map.entry("zero", "0"),
…);
// …
final StringBuilder sb = new StringBuilder(digitwords.size());
for (final String digit : digitwords) {
sb.append(digitmap.get(digit));
}
final String result = sb.toString();