Skip to content

Instantly share code, notes, and snippets.

@RustyKnight
Last active March 30, 2022 21:40
Show Gist options
  • Save RustyKnight/93f07a59b1b17af7da9d7a5f15aa79a1 to your computer and use it in GitHub Desktop.
Save RustyKnight/93f07a59b1b17af7da9d7a5f15aa79a1 to your computer and use it in GitHub Desktop.
A conceptually terminal parser for Java using `Scanner`
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;
}
}
@RustyKnight
Copy link
Author

For example...

TerminalInput input = new TerminalInput();

System.out.println("Enter interger");
System.out.println(input.getInt());
System.out.println("Enter some interger");
System.out.println(input.getInts());
System.out.println("Enter some more interger");
System.out.println(input.getAllAvaliableInts());

might output something like...

Enter interger
1
1
Enter some interger
1 2 3 4 5 6 7 a 8 9
[1, 2, 3, 4, 5, 6, 7]
Enter some more interger
a 1 b 2 c 3 d 4
[1, 2, 3, 4]

@RustyKnight
Copy link
Author

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);

@RustyKnight
Copy link
Author

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