Last active
March 30, 2022 21:40
-
-
Save RustyKnight/93f07a59b1b17af7da9d7a5f15aa79a1 to your computer and use it in GitHub Desktop.
A conceptually terminal parser for Java using `Scanner`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TerminalInput { | |
private Scanner terminalScanner; | |
public TerminalInput() { | |
terminalScanner = new Scanner(System.in); | |
} | |
public String getNextLine() { | |
return terminalScanner.nextLine(); | |
} | |
protected Scanner nextLine() { | |
return new Scanner(getNextLine()); | |
} | |
public String parseNextToken(Scanner scanner) { | |
return scanner.next(); | |
} | |
public Integer parseNextInt(Scanner scanner) { | |
if (scanner.hasNextInt()) { | |
return scanner.nextInt(); | |
} | |
return null; | |
} | |
/** | |
* Get the next int value from the terminal input. This will consume the | |
* next line of text and return a int value from the start of the text | |
* or null if none exists | |
* | |
* @return Integer value or null of no int exists | |
*/ | |
public Integer getInt() { | |
return parseNextInt(nextLine()); | |
} | |
/** | |
* Returns all the integers up to the first non-int value. For example | |
* "1 2 3 4 a 5 6 7 8 9 0" will return `[1, 2, 3, 4]` | |
* | |
* @return A list of integers | |
*/ | |
public List<Integer> getInts() { | |
Scanner scanner = nextLine(); | |
List<Integer> items = new ArrayList<>(32); | |
while (scanner.hasNextInt()) { | |
items.add(scanner.nextInt()); | |
} | |
return items; | |
} | |
/** | |
* Returns ALL the integer values input on a single line Returns all the | |
* integers up to the first non-int value. For example "1 2 3 4 a 5 6 7 | |
* 8 9 0" will return `[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]` | |
* | |
* @return | |
*/ | |
public List<Integer> getAllAvaliableInts() { | |
Scanner scanner = nextLine(); | |
List<Integer> items = new ArrayList<>(32); | |
while (scanner.hasNext()) { | |
if (scanner.hasNextInt()) { | |
items.add(scanner.nextInt()); | |
} else { | |
// Just consume the input | |
scanner.next(); | |
} | |
} | |
return items; | |
} | |
} |
Requesting input and validing the result might look something like...
TerminalInput input = new TerminalInput();
Integer value = null;
do {
System.out.print("Please enter a value between 1-4");
value = input.getInt();
if (value == null) {
System.out.println("!! Invalid input");
} else if (value < 1 || value > 4) {
System.out.println("!! Invalid input");
value = null;
}
} while (value == null);
System.out.println("You have selected " + value);
What if you wanted to provide a different (non-integer) exit value?
Now you're just trying to make my life more difficult 😓
TerminalInput input = new TerminalInput();
Integer value = null;
boolean done = false;
do {
System.out.print("Please enter a value between 1-4, [E]xit");
Scanner scanner = input.nextLine();
value = input.parseNextInt(scanner);
if (value == null) {
String text = input.parseNextToken(scanner);
if ("e".equalsIgnoreCase(text)) {
done = true;
} else {
System.out.println("!! Invalid input");
}
} else if (value < 1 || value > 4) {
System.out.println("!! Invalid input");
value = null;
} else {
done = true;
}
} while (!done);
if (value != null) {
System.out.println("You have selected " + value);
} else {
System.out.println("You did exit");
}
Note, TerminalInput#nextLine
returns a Scanner
, so you don't "have" to make use of parseNextToken
, you could just use the Scanner
itself
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For example...
might output something like...