我得到了下面的代碼:的Java Scanner類閱讀串
int nnames;
String names[];
System.out.print("How many names are you going to save: ");
Scanner in = new Scanner(System.in);
nnames = in.nextInt();
names = new String[nnames];
for (int i = 0; i < names.length; i++){
System.out.print("Type a name: ");
names[i] = in.next();
}
System.out.println(names[0]);
當我運行這段代碼,掃描儀將只拿起第一個名字,而不是姓氏。當試圖輸入一個名字時,它有時會跳過一行,它會顯示出來,就好像我把名字留空了一樣,然後跳到下一個名字。我不知道是什麼原因造成的。
我希望有人能幫助我!
編輯:我曾嘗試in.nextLine();它修復了全名,但它仍然保持一條線,這裏是輸出的一個例子:
How many names are you going to save: 3
Type a name: Type a name: John Doe
Type a name: John Lennon
完全匹配http://stackoverflow.com/questions/1466418/java-scanner-class-reading-strings – CPerkins 2009-09-23 19:36:44