雖然聽起來很愚蠢的風險,這裏,我去:如何驗證是否鍵盤輸入被賦予
誰能告訴我如何驗證一個字符串是通過掃描儀輸入的?
我在下面寫了一些代碼,但我卡住了。你可能會注意到,我是一個完整的新手。
import static java.lang.System.in;
import static java.lang.System.out;
import java.util.Scanner;
public class Test {
public static void main(String args[]) {
Scanner keyboard = new Scanner(in);
char reply;
do {
System.out.print("Nieuwe klant? (j/n)");
reply = keyboard.findWithinHorizon(".", 0).charAt(0);
} while (reply != 'j' && reply != 'n');
String giveFirstName;
String giveLastName;
String giveStreet;
String giveNumber;
String giveCity;
String giveLand;
String giveMail;
String giveGsm;
String enterLastName;
String enterFirstName;
if (reply == 'j') {
System.out.print("Voornaam Klant: ");
giveFirstName = keyboard.next();
System.out.print("Achternaam Klant: ");
giveLastName = keyboard.next();
System.out.print("Straatnaam: ");
giveStreet = keyboard.next();
System.out.print("nummer: ");
giveNumber = keyboard.next();
System.out.print("Stad: ");
giveCity = keyboard.next();
System.out.print("Land: ");
giveLand = keyboard.next();
System.out.print("E-mail adress: ");
giveMail = keyboard.next();
System.out.print("Gsm-nummer: ");
giveGsm = keyboard.next();
} else {
out.print("Voornaam bestaande klant:");
enterFirstName = keyboard.next();
System.out.print("Achternaam bestaande klant:");
enterLastName = keyboard.next();
}
}
}
你是什麼意思「驗證是否輸入了字符串」?只要用戶不輸入任何內容,掃描儀就不會返回任何內容,如果這就是你的意思。 – Paul
驗證是實際上繼續執行某些操作的代碼,因爲'Scanner#next()'確實等待輸入(但是如果有超過1個由下一個標記標識的值, case'Input1 Input2')。 – SomeJavaGuy
好保羅,我的意圖是檢查用戶是否在繼續該程序之前給出了輸入 –