Here is a helper class that I created for getting input from the console in java. Created a java class named InputHelper and paste the content below and press ctrl+shift+f to format it.
private static BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
/**
* Accepts valid input from the user such as true or 0 and any negative or
* positive number.
*
* @param display
* what to prompt the user before accepting input
* @return {@link Boolean} of the users response
*/
public static boolean getBoolean(String display) {
boolean validInput = false;
boolean result = false;
while (validInput) {
System.out.print(display);
String input = getString();
if (input.equalsIgnoreCase("true") || input.equalsIgnoreCase("0")) {
result = true;
validInput = true;
} else if (input.equalsIgnoreCase("-?\\d+")
|| input.equalsIgnoreCase("false")) {
result = false;
validInput = true;
}
}
return result;
}
/**
* Get's the input from the user as a {@link Short}. The input must be
* between the lower and upper bounds specified inclusive. If not between
* the values the user will be prompted with the error
* "Please input a number between (lower) and (upper) inclusive." The user
* will continue to be prompted with the message to display until valid
* input is given.
*
* @param lower
* bound
* @param upper
* bound
* @param display
* what to prompt the user before accepting input
* @return input from the user as a {@link Short}
*/
public static short getShort(short lower, short upper, String display) {
boolean valid = false;
short result = 0;
while (!valid) {
System.out.print(display);
String input = getString();
if (isWholeNumber(input)) {
result = Short.parseShort(input);
valid = withinBounds(lower, upper, result);
}
}
return result;
}
/**
* Get's the input from the user as a {@link Integer}. The input must be
* between the lower and upper bounds specified inclusive. If not between
* the values the user will be prompted with the error
* "Please input a number between (lower) and (upper) inclusive." The user
* will continue to be prompted with the message to display until valid
* input is given.
*
* @param lower
* bound
* @param upper
* bound
* @param display
* what to prompt the user before accepting input
* @return input from the user as a {@link Integer}
*/
public static int getInt(int lower, int upper, String display) {
boolean valid = false;
int result = 0;
while (!valid) {
System.out.print(display);
String input = getString();
if (isWholeNumber(input)) {
result = Integer.parseInt(input);
valid = withinBounds(lower, upper, result);
}
}
return result;
}
/**
* Get's the input from the user as a {@link Long}. The input must be
* between the lower and upper bounds specified inclusive. If not between
* the values the user will be prompted with the error
* "Please input a number between (lower) and (upper) inclusive." The user
* will continue to be prompted with the message to display until valid
* input is given.
*
* @param long lower bound
* @param long upper bound
* @param String
* display what to prompt the user before accepting input
* @return input from the user as a {@link Long}
*/
public static long getLong(long lower, long upper, String display) {
boolean valid = false;
long result = 0;
while (!valid) {
System.out.print(display);
String input = getString();
if (isWholeNumber(input)) {
result = Long.parseLong(input);
valid = withinBounds(lower, upper, result);
}
}
return result;
}
/**
* Get's the input from the user as a {@link Double}. The input must be
* between the lower and upper bounds specified inclusive. If not between
* the values the user will be prompted with the error
* "Please input a number between (lower) and (upper) inclusive." The user
* will continue to be prompted with the message to display until valid
* input is given.
*
* @param lower
* bound
* @param upper
* bound
* @param display
* what to prompt the user before accepting input
* @return input from the user as a {@link Double}
*/
public static double getDouble(double lower, double upper, String display) {
boolean valid = false;
double result = 0;
while (!valid) {
System.out.print(display);
String input = getString();
if (isFractional(input)) {
result = Double.parseDouble(input);
valid = withinBounds(lower, upper, result);
}
}
return result;
}
/**
* Get's the input from the user as a {@link Float}. The input must be
* between the lower and upper bounds specified inclusive. If not between
* the values the user will be prompted with the error
* "Please input a number between (lower) and (upper) inclusive." The user
* will continue to be prompted with the message to display until valid
* input is given.
*
* @param lower
* bound
* @param upper
* bound
* @param display
* what to prompt the user before accepting input
* @return input from the user as a {@link Float}
*/
public static float getFloat(float lower, float upper, String display) {
boolean valid = false;
float result = 0;
while (!valid) {
System.out.print(display);
String input = getString();
if (isFractional(input)) {
result = Float.parseFloat(input);
valid = withinBounds(lower, upper, result);
}
}
return result;
}
/**
* Get's the input from the user as a {@link String}.
*
* @return {@link String} representation of the input from the user
*/
public static String getString() {
String input = new String();
try {
input = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return input;
}
/**
* Prints on a new line.
*
* @param message
* to be printed
*/
public static void println(String message) {
System.out.println(message);
}
/**
* Prints on the same line.
*
* @param message
* to be printed
*/
public static void print(String message) {
System.out.print(message);
}
/**
* Performs a check to see if the input consists of only digits
*
* @param input
* as a string
* @return boolean indicating if the number consists of only digits(true) or
* not(false)
*/
private static boolean isWholeNumber(String input) {
boolean isWhole = false;
if (input.matches("-?\\d+")) {
isWhole = true;
} else {
System.out.println("Please enter whole digits only.");
}
return isWhole;
}
/**
* Checks to see if the number is or is not a whole number. Examples of
* valid input are as follows: .0, 0.0, 132.2134, -12.2321 Currently it
* does not handle a plain integer.
*
* @param input
* string representation of input
* @return whether the number is not whole(true) or whole(false)
*/
private static boolean isFractional(String input) {
boolean isFractional = false;
if (input.matches("-?\\d*\\.\\d+")) {
isFractional = true;
} else {
System.out.println("Please enter decimal digits only.");
}
return isFractional;
}
/**
* Performs a check to see if the number is in between the bounds inclusive.
* If so then the check will return true. Otherwise the check will print
* "Please input a number between (lower) and (upper) inclusive." and return
* false.
*
* @param lower
* bound
* @param upper
* bound
* @param num
* to check between bounds
* @return if number is within bounds inclusive(true) else(false)
*/
private static boolean withinBounds(double lower, double upper, double num) {
boolean result = false;
if (num >= lower && num <= upper) {
result = true;
} else {
printRangeWarning("" + lower, "" + upper);
}
return result;
}
/**
* Outputs an error message specifying the range of the numbers.
*
* @param lower
* bound
* @param upper
* bound
*/
private static void printRangeWarning(String lower, String upper) {
String toPrint = "Please input a number between (" + lower + ") and ("
+ upper + ") inclusive.";
System.out.println(toPrint);
}
No comments:
Post a Comment