Package dev.davwheat
Class IOHelper
- java.lang.Object
-
- dev.davwheat.IOHelper
-
public class IOHelper extends java.lang.ObjectClass to make inputting data much easier.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Scannerscannerstatic java.util.function.Function<java.lang.Character,java.lang.Boolean>YesNoCharValidatorPre-created custom char validator used to only accept Y/N inputs.
-
Constructor Summary
Constructors Constructor Description IOHelper()
-
Method Summary
Modifier and Type Method Description voidpressEnterToContinue()Pauses execution until the user presses ENTER.charreadChar(java.lang.String message, java.lang.String failMessage)Reads a char until it passes the validationcharreadChar(java.lang.String message, java.lang.String failMessage, java.util.function.Function<java.lang.Character,java.lang.Boolean> customValidator)Reads a char until it passes the validationintreadInteger(java.lang.String message, java.lang.String failMessage)Reads an int until it passes the validationintreadInteger(java.lang.String message, java.lang.String failMessage, java.util.function.Function<java.lang.Integer,java.lang.Boolean> customValidator)Reads an int until it passes the validationjava.lang.StringreadString(java.lang.String message, java.lang.String failMessage)Reads a string until it passes the validationjava.lang.StringreadString(java.lang.String message, java.lang.String failMessage, java.util.function.Function<java.lang.String,java.lang.Boolean> customValidator)Reads a string until it passes the validationvoidsetScanner(java.util.Scanner scanner)Used in tests to allow a custom Scanner to be set.
-
-
-
Method Detail
-
setScanner
public void setScanner(java.util.Scanner scanner)
Used in tests to allow a custom Scanner to be set.This allows us to provide custom input to the IOHelper functions.
Do not use this in normal code.
- Parameters:
scanner- custom scanner
-
readString
public java.lang.String readString(java.lang.String message, java.lang.String failMessage)Reads a string until it passes the validation- Parameters:
message- Message to output before input is readfailMessage- Message to output if validation fails- Returns:
- The inputted string
-
readString
public java.lang.String readString(java.lang.String message, java.lang.String failMessage, java.util.function.Function<java.lang.String,java.lang.Boolean> customValidator)Reads a string until it passes the validation- Parameters:
message- Message to output before input is readfailMessage- Message to output if validation failscustomValidator- Custom string validator- Returns:
- The inputted string
-
readInteger
public int readInteger(java.lang.String message, java.lang.String failMessage)Reads an int until it passes the validation- Parameters:
message- Message to output before input is readfailMessage- Message to output if validation fails- Returns:
- The inputted integer
-
readInteger
public int readInteger(java.lang.String message, java.lang.String failMessage, java.util.function.Function<java.lang.Integer,java.lang.Boolean> customValidator)Reads an int until it passes the validation- Parameters:
message- Message to output before input is readfailMessage- Message to output if validation failscustomValidator- Custom validator- Returns:
- The inputted integer
-
readChar
public char readChar(java.lang.String message, java.lang.String failMessage)Reads a char until it passes the validation- Parameters:
message- Message to output before input is readfailMessage- Message to output if validation fails- Returns:
- The inputted char
-
readChar
public char readChar(java.lang.String message, java.lang.String failMessage, java.util.function.Function<java.lang.Character,java.lang.Boolean> customValidator)Reads a char until it passes the validation- Parameters:
message- Message to output before input is readfailMessage- Message to output if validation failscustomValidator- Custom char validator- Returns:
- string The inputted char
-
pressEnterToContinue
public void pressEnterToContinue()
Pauses execution until the user presses ENTER.Does not display any prompts, so ensure that you inform the user that they should press ENTER before you call this.
-
-