A junior dev on the team had a simple question: in Java, how do I correctly read user input from the CLI and act on it?

Now, we can wonder about a junior dev not having such basic knowledge, but maybe they're new to Java specifically. Who knows. But Robert's team has a senior on it who "likes to mentor". This senior has strong opinions about how to write software, extreme confidence in those opinions, and has somehow been around the company long enough that he knows where the bodies are buried and no one will fire him.

Here's his sample application for reading input.

import java.nio.charset.StandardCharsets;
import java.util.*;

public class ScannerTool {
  public static void main(String[] args) {
    Map<Optional<Character>, Map.Entry<String, Boolean>> map = new HashMap<>();
    map.put(Optional.of('A'), new AbstractMap.SimpleEntry<>("You entered A.", true));
    map.put(Optional.of('B'), new AbstractMap.SimpleEntry<>("You entered B.", true));
    map.put(Optional.empty(), new AbstractMap.SimpleEntry<>("You entered nothing.", false));
    Optional<Character> input;
    do {
      System.out.println("Please enter A or B:");
    } while (!map.getOrDefault((input = nextCharacter()), new AbstractMap.SimpleEntry<>("", false))
        .getValue());
    System.out.println(map.get(input).getKey());
  }

  public static Optional<Character> nextCharacter() {
    String l = new Scanner(System.in, StandardCharsets.UTF_8).nextLine();
    if (l != null && !l.isEmpty()) {
      return Optional.of(l.charAt(0));
    }
    return Optional.empty();
  }
}

Look, I know Java is verbose, but this is entirely next level. Giving this to a junior has to constitute an attempt to drive the youth out of the industry, because any sane person being told, "your job is to write this" would realize they've committed a grave error in their career choice and needs to escape now.

nextCharacter in this case actually returns the first character of the next line, not the next character. And if there isn't one, it returns an empty optional. Which, fine, we only expect one character per line of input for this exercise, so that's not wrong, I just don't like the name of the function.

Of course, using Scanner here is a wrecking ball fly swatter- that class is a full on parser for reading out common data types from a file. Worth showing to a junior, but a little out of scope of this question.

But the real overengineering is the map of maps- well, a map of map entries. The key to the outer map is an optional of the character that was entered by the user. The value is itself a map, the key of the map is the message we want to output, the value is whether or not the loop should continue. They've reinvented tuples using map entries. Which, in their defense, mainline Java doesn't have tuples (enterprise edition seems to have them in the javax namespace, but I'm not even certain those are meant to be general purpose tuples anyway). A quick search indicates that their absence is a perennial source of heated discussions in forums and Stack Overflow.

Robert writes:

It is remarkable in its creativity and execution and still has me in awe. When I tried to unpack it, my brain just started to pack up its things and left, haven't seen it since.

Bon voyage.

[Advertisement] Picking up NuGet is easy. Getting good at it takes time. Download our guide to learn the best practice of NuGet for the Enterprise.