Class SuperScanner

Object
SuperScanner

public class SuperScanner extends Object
An extension of features for the Scanner class. This class allows for more convenient input gathering from users, for types that may be error prone. For example, when expecting an integer, the user may input something else, such as text. Here, it will automatically and recursively ask the user until the input given is in correct format.
  • Field Details

    • scanner

      private Scanner scanner
      Scanner object to be referenced in class.
  • Constructor Details

    • SuperScanner

      public SuperScanner(Scanner scanner)
      Construct a SuperScanner to use convenient user input abilities
      Parameters:
      scanner - Scanner to be used to get user input.
  • Method Details

    • getScanner

      public Scanner getScanner()
      Where necessary, you can access the associated Scanner object where necessary.
      Returns:
      Scanner object.
    • nextIntUntilCorrect

      public int nextIntUntilCorrect(String prompt)
      Get input from user until input is a number.
      Parameters:
      prompt - The prompt to be shown to the user.
      Returns:
      integer input by user.
    • nextIntUntilCorrect

      public int nextIntUntilCorrect(String prompt, int min, int max)
      Get input from user until input is a number, and falls within boundaries provided. Both min and max parameters are inclusive; All values in between are also treated as accepted.
      Parameters:
      prompt - The prompt to be shown to the user.
      min - The minimum value to be accepted. (inclusive)
      max - The maximum value to be accepted. (inclusive)
      Returns:
      The integer that is provided by the user's input.
    • nextDateUntilCorrect

      public LocalDate nextDateUntilCorrect(String prompt)
      Get user input of date type, while ensuring format is correct. Date format expected should be d/M/yy, where d is day, M is month, and yy is year (2 digits)
      Parameters:
      prompt - The prompt to be shown to the user.
      Returns:
      Date provided by the user.
    • nextBoolUntilCorrect

      public Boolean nextBoolUntilCorrect(String prompt)
      Get user input that is binary (either true or false). Repeats until user input given is valid. This method accepts a few forms to represent truthness.
      • true:
        • T
        • True
        • Yes
        • Y
      • false:
        • F
        • False
        • No
        • N
      Parameters:
      prompt - The prompt to be shown to the user.
      Returns:
      boolean representative of user's input.
    • nextInt

      private int nextInt(String prompt) throws NumberFormatException
      Prints the prompt, and then wait for user's input, on same line, expecting a number.
      Parameters:
      prompt - The prompt associated with the input.
      Returns:
      User's input as an integer.
      Throws:
      NumberFormatException - When user's input is not an integer.
    • nextDate

      private LocalDate nextDate(String prompt)
      Prints the prompt, and then wait for user's input, on same line, expecting a date, of d/M/yy. d/M/yy format is used as that is the date format that the spreadsheet samples given to us are using.
      Parameters:
      prompt - The prompt associated with the input.
      Returns:
      User's input as an date (format: d/M/yy).
      Throws:
      DateTimeParseException - When user's input is not a date or of incorrect formatting.
    • nextBool

      private Boolean nextBool(String prompt) throws IllegalArgumentException
      Prints the prompt, and then wait for user's input, on same line, expecting a boolean. This method employs a very relaxed way of detecting boolean state. Users can input the following, irrespective of case:
      • true:
        • T
        • True
        • Yes
        • Y
      • false:
        • F
        • False
        • No
        • N
      Parameters:
      prompt - The prompt associated with the input.
      Returns:
      A boolean according to user's input
      Throws:
      IllegalArgumentException - When user's input does not fit detectable boolean state.