This class is intended as a small and simple alternative to java.util.Scanner for people in the early stages of learning Java. Unlike Scanner it has built-in error handling and retry, it throws no checked exceptions, is as tolerant as possible of variations in the format of user input, and avoids “gotchas” like Scanner’s nextInt/nextLine.
Using it is as simple as
UserInput ui = new UserInput();
String name = iu.getString(“Enter name:”);
int age = ui.getInt(“Enter age:”);
double salary = ui.getDouble(“Enter salary:”);
The isYes(String prompt)
method returns a boolean suitable for yes/no questions, e.g.
do {
…
while (ui.isYes(“enter more data?”));
An optional feature allows you to save all the user input in a text file, and to take input from a text file for automated testing.
See the javadoc for details of all the methods.
This class was inspired by Daniweb member David W’s pathfinding work on defining and coding a simpler Scanner, and completed by assertnull and JamesCherrill. It is is free and unencumbered software released into the public domain