Inputer is located under the directory: "src/com/biziol/Inputer.java"
Once you have made the changes, compile the class
go to the build folder and run this command:
cd bin && jar cvf ../InputerLibrary.jar com/biziol/Inputer.class
: this will create the library needed by users.
This library, Inputer
, provides a convenient way to read various types of input from the console in Java.
- Place the
InputerLibrary.jar
file into thelib
folder of your Java project. - Import library via
import com.biziol.Inputer
To use the Inputer
class in your project, import it:
import com.biziol.Inputer;
Then, create an instance of the Inputer class:
Inputer inputer = new Inputer();
Description: Reads a line of text from the console.
Return Value: A String containing the line read from the console, or null if the end of the stream is reached.
Example:
String input = inputer.readLine();
System.out.println("You entered: " + input);
Description: Displays a prompt and reads a line of text from the console.
Parameters:
prompt (String): The prompt to display to the user. Return Value: A String containing the line read from the console, or null if the end of the stream is reached.
Example:
String name = inputer.readLine("Enter your name: ");
System.out.println("Your name is: " + name);
Description: Reads a password from the console with echoing disabled.
Return Value: A char[] containing the password, or null if the end of the stream is reached.
Example:
char[] password = inputer.readPasswd();
// Remember to clear the password array after use for security!
java.util.Arrays.fill(password, ' ');
Description: Displays a prompt and reads a password from the console with echoing disabled.
Parameters:
prompt (String): The prompt to display to the user. Return Value: A char[] containing the password, or null if the end of the stream is reached.
Example:
char[] password = inputer.readPasswd("Enter your password: ");
// Remember to clear the password array after use for security!
java.util.Arrays.fill(password, ' ');
Description: Reads an integer from the console.
Return Value: An Integer value, or null if the input is not a valid integer.
Error Handling: If the input is not a valid integer, an error message is printed to System.err.
Example:
Integer number = inputer.readInteger();
if (number != null) {
System.out.println("You entered: " + number);
}
Description: Displays a prompt and reads an integer from the console.
Parameters:
prompt (String): The prompt to display to the user. Return Value: An Integer value, or null if the input is not a valid integer.
Error Handling: If the input is not a valid integer, an error message is printed to System.err.
Example:
Integer age = inputer.readInteger("Enter your age: ");
if (age != null) {
System.out.println("Your age is: " + age);
}
Description: Reads a double from the console.
Return Value: A Double value, or null if the input is not a valid double.
Error Handling: If the input is not a valid double, an error message is printed to System.err.
Example:
Double value = inputer.readDouble();
if (value != null) {
System.out.println("You entered: " + value);
}
Description: Displays a prompt and reads a double from the console.
Parameters:
prompt (String): The prompt to display to the user. Return Value: A Double value, or null if the input is not a valid double.
Error Handling: If the input is not a valid double, an error message is printed to System.err.
Example:
Double price = inputer.readDouble("Enter the price: ");
if (price != null) {
System.out.println("The price is: " + price);
}
Description: Returns the Reader associated with the console.
Return Value: A Reader object.
Example:
Reader reader = inputer.reader();
// Use the reader to read input...
Description: Clears the console screen.
Example:
Inputer.clearScreen();
The readInteger() and readDouble() methods handle NumberFormatException by printing an error message to System.err and returning null. You should check for null after calling these methods to handle invalid input.