これは、コマンド ライン アプリケーションでキーボード入力を読み取るために Scanner の代わりに使用する Console クラスです。クラスは、Java 1.6 で導入された標準の java.io.Console を「ラップ」することに注意してください。
コンソール クラスから必要なメソッドだけを掘り出して、クラスに直接配置することLab8
もできますが、独自の「キーボード」クラスを作成して、後でではなく別のクラスに「懸念事項を分離する」ことに慣れることをお勧めします。 、私のコンソールの縮小版として。
package krc.utilz.io;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
/**
* A static library of helper methods to read keyboard input.
* <p>
* <strong>usage:</strong>
* <code>
* import krc.utilz.io.Console;
* while ( (score=Console.readInteger("Enter an integer between 0 and 100 (enter to exit) : ", 0, 100, -1)) != -1) { ... }
* </code>
*/
public abstract class Console
{
private static final java.io.Console theConsole = System.console();
static {
if ( theConsole == null ) {
System.err.println("krc.utilz.io.Console: No system console!");
System.exit(2); // 2 traditionally means "system error" on unix.
}
}
private static final DateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy");
private static final DateFormat timeFormatter = new SimpleDateFormat("HH:mm:ss");
public static String readString(String prompt) {
String response = readLine(prompt);
if(response==null) throw new NullPointerException("response cannot be null");
return response;
}
public static String readLine(String prompt) {
if (!prompt.endsWith(" ")) prompt += " ";
System.out.print(prompt);
return theConsole.readLine();
}
public static String readString(String prompt, String regex) {
while(true) {
String response = readString(prompt);
if ( response.length() > 0 ) {
if ( response.matches(regex) ) {
return response;
}
}
System.out.println("Oops: A match for "+regex+" is required!");
}
}
public static Date readDate(String prompt) {
while(true) {
try {
return dateFormatter.parse(readString(prompt+" (dd/MM/yyyy) : "));
} catch (java.text.ParseException e) {
System.out.println("Oops: "+e);
}
}
}
public static Date readTime(String prompt) {
while(true) {
try {
String response = readWord(prompt+" (HH:mm:ss) : ");
return timeFormatter.parse(response);
} catch (java.text.ParseException e) {
System.out.println("Oops: "+e);
}
}
}
public static String readWord(String prompt) {
while(true) {
String response = readString(prompt);
if(response.length()>0 && response.indexOf(' ')<0) return response;
System.out.println("Oops: A single word is required. No spaces.");
}
}
public static String readWordOrNull(String prompt) {
while(true) {
String response = readLine(prompt);
if(response==null||response.length()==0) return null;
if(response.indexOf(' ')<0 ) return response;
System.out.println("Oops: A single word is required. No spaces.");
}
}
public static char readChar(String prompt) {
while ( true ) {
String response = readString(prompt);
if ( response.trim().length() == 1 ) {
return response.trim().charAt(0);
}
System.out.println("Oops: A single non-whitespace character is required!");
}
}
public static char readLetter(String prompt) {
while(true) {
String response = readString(prompt);
if ( response.trim().length() == 1 ) {
char result = response.trim().charAt(0);
if(Character.isLetter(result)) return result;
}
System.out.println("Oops: A single letter is required!");
}
}
public static char readDigit(String prompt) {
while(true) {
String response = readString(prompt);
if ( response.trim().length() == 1 ) {
char result = response.trim().charAt(0);
if(Character.isDigit(result)) return result;
}
System.out.println("Oops: A single digit is required!");
}
}
public static int readInteger(String prompt) {
String response = null;
while(true) {
try {
response = readString(prompt);
if ( response.length()>0 ) {
return Integer.parseInt(response);
}
System.out.println("An integer is required.");
} catch (NumberFormatException e) {
System.out.println("Oops \""+response+"\" cannot be interpreted as 32-bit signed integer!");
}
}
}
public static int readInteger(String prompt, int lowerBound, int upperBound) {
int result = 0;
while(true) {
result = readInteger(prompt);
if ( result>=lowerBound && result<=upperBound ) break;
System.out.println("An integer between "+lowerBound+" and "+upperBound+" (inclusive) is required.");
}
return(result);
}
public static int readInteger(String prompt, int defaultValue) {
String response = null;
while(true) {
try {
response = readString(prompt);
return response.trim().length()>0 ? Integer.parseInt(response) : defaultValue;
} catch (NumberFormatException e) {
System.out.println("Oops \""+response+"\" cannot be interpreted as 32-bit signed integer!");
}
}
}
public static int readInteger(String prompt, int lowerBound, int upperBound, int defaultValue) {
int result = 0;
while(true) {
result = readInteger(prompt, defaultValue);
if ( result==defaultValue || result>=lowerBound && result<=upperBound ) break;
System.out.println("An integer between "+lowerBound+" and "+upperBound+" (inclusive) is required.");
}
return(result);
}
public static double readDouble(String fmt, Object... args) {
String response = null;
while(true) {
try {
response = System.console().readLine(fmt, args);
if ( response!=null && response.length()>0 ) {
return Double.parseDouble(response);
}
System.out.println("A number is required.");
} catch (NumberFormatException e) {
System.out.println("\""+response+"\" cannot be interpreted as a number!");
}
}
}
public static double readDouble(String prompt, double lowerBound, double upperBound) {
while(true) {
double result = readDouble(prompt);
if ( result>=lowerBound && result<=upperBound ) {
return(result);
}
System.out.println("A number between "+lowerBound+" and "+upperBound+" (inclusive) is required.");
}
}
public static boolean readBoolean(String prompt) {
String response = readString(prompt+" (y/N) : ");
return response.trim().equalsIgnoreCase("Y");
}
public static java.io.Console systemConsole() {
return theConsole;
}
}
乾杯。キース。
PS: これは練習すればずっと簡単になります...うまくいきます...ただトラックインを続けてください.