2011-09-15 15 views
3

我一直在試圖弄清楚如何使用Scanner類一次輸入多個標記。我發現一些完美的代碼。我知道Scanner.hasNext方法可以無限期阻止。爲什麼在這段代碼中的行keyboard = new Scanner(keyboard.nextLine());阻止它這樣做?爲什麼線鍵盤=新掃描儀(keyboard.nextLine());保持程序掛起?

Scanner keyboard = new Scanner(System.in); 
    LinkedList<String> ll = new LinkedList<String>(); 

    System.out.println("Please enter your full name: "); 
    keyboard = new Scanner(keyboard.nextLine()); 

    while(keyboard.hasNext()) 
    { 
     System.out.println("tag "); 
     ll.add(keyboard.next()); 

    } 

    System.out.println(ll); 

謝謝!

回答

5

keyboard將是從第一行輸入讀取標記的Scanner

當您使用Scanner(String str)構造函數時,生成的掃描程序將使用str作爲輸入。

如果這很清楚,您可能只需要瞭解終端IO是線路緩衝的。這意味着掃描儀在您按返回之前沒有任何可讀的內容。