Made the interactive binary entropy reader more forgiving for a better UX

This commit is contained in:
B.J. Dweck 2021-03-14 10:14:19 +02:00
parent dc6179dcd0
commit 4fd4bb407b

View File

@ -12,15 +12,31 @@ public class InteractiveBinaryEntropyGenerator extends InteractiveEntropyGenerat
@Override @Override
protected String readNextEventSetString() { protected String readNextEventSetString() {
String eventSetString = ""; StringBuilder eventSetBuffer = new StringBuilder();
Pattern eventSetPattern = Pattern.compile(String.format("[0-1]{%d}", eventsPerEventSet)); int remainingEvents = eventsPerEventSet;
Pattern eventSetPattern = Pattern.compile("[01]+");
while (!eventSetPattern.matcher(eventSetString).matches()) { while (remainingEvents > 0) {
System.out.print("Input 33 coin tosses [0-1]: ");
eventSetString = inputScanner.next(); System.out.printf("Input %d coin tosses [0-1]: ", remainingEvents);
String nextInput = inputScanner.next();
if (!eventSetPattern.matcher(nextInput).matches()) {
System.out.printf(
"Invalid input! Enter %d-digit sequence of 1's and 0's (for example, 1011101010)" + System.lineSeparator(),
remainingEvents);
continue;
} }
return eventSetString; eventSetBuffer.append(nextInput);
remainingEvents -= nextInput.length();
}
if (remainingEvents < 0)
System.out.printf("NOTE: Discarding the %d extra coin toss(es) entered" + System.lineSeparator(), -remainingEvents);
return eventSetBuffer.substring(0, eventsPerEventSet);
} }
@Override @Override