Created
June 7, 2014 00:18
-
-
Save scottoffen/c0520cfc81e9727d5fcb to your computer and use it in GitHub Desktop.
Java Command Line Made Easy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
public class CommandLine | |
{ | |
private static final BufferedReader input; | |
static | |
{ | |
input = new BufferedReader(new InputStreamReader(System.in)); | |
} | |
private CommandLine() {}; | |
public static String getUserInput (String message) | |
{ | |
System.out.print(message); | |
String value = null; | |
try | |
{ | |
value = input.readLine(); | |
} | |
catch (IOException e) | |
{ | |
System.out.println("Error reading input from user : " + e.getMessage()); | |
} | |
return value; | |
} | |
public static String getUserInput (String message, int maxLen) | |
{ | |
boolean loop = true; | |
String value = null; | |
while (loop) | |
{ | |
loop = false; | |
value = getUserInput(message); | |
if (value.length() > maxLen) | |
{ | |
loop = true; | |
System.out.println(); | |
System.out.println("Input must be less than " + maxLen + " characters."); | |
} | |
else | |
{ | |
loop = false; | |
} | |
} | |
return value; | |
} | |
public static int getNumberInRange(String message, int min, int max) | |
{ | |
boolean loop = true; | |
int value = 0; | |
while (loop) | |
{ | |
loop = false; | |
String input = getUserInput(message); | |
value = Integer.parseInt(input, 10); | |
if ((value > max) || (value < min)) | |
{ | |
loop = true; | |
String alert = (value > max) ? "Value must be less than or equal to " + max : "Value must be more than or equal to " + min; | |
System.out.println(); | |
System.out.println(alert); | |
} | |
} | |
return value; | |
} | |
public static boolean getUserInput (String message, String tv) | |
{ | |
String value = getUserInput(message); | |
return (value.equalsIgnoreCase(tv)) ? true : false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment