2012-05-15 22 views
18

我有這樣的事情:如何清除Java中的掃描緩衝區?

Scanner in=new Scanner(System.in); 
    int rounds = 0; 
    while (rounds < 1 || rounds > 3) { 
     System.out.print("How many rounds? "); 
     if (in.hasNextInt()) { 
      rounds = in.nextInt(); 
     } else { 
      System.out.println("Invalid input. Please try again."); 
      System.out.println(); 
     } 
     // Clear buffer 
    } 
    System.out.print(rounds+" rounds."); 

如何清除緩衝區?

編輯:我嘗試以下,但它並不適用於某些原因:

while(in.hasNext()) 
    in.next(); 
+0

你是什麼意思與 「清除緩存」?清除控制檯(如cls dos命令)? –

+1

我正在尋找一些內容:'while(in.hasNext())in.next();'。出於某種原因,該循環不起作用。 –

回答

6

您無法明確地清除掃描儀的緩衝區。在內部,它可以在讀取令牌之後清除緩衝區,但是這是實施細節以外的實施細節。

23

試試這個:

in.nextLine(); 

這前進掃描到下一行。

+3

有時候會有多行輸入,這隻適用於一行。 –

+2

輸入儘可能多的行。 – m0skit0

+0

您可能會認爲hasNextLine(或hasNext)可用於檢查是否有更多輸入可用,但這在Java中不起作用。 hasNextLine/hasNext方法始終對鍵盤(System.in)返回true。我發現清除所有緩衝數據而沒有阻塞的唯一方法是打開一個新的掃描器:in.close(); in = new Scanner(System.in); – Georgie

0

插入線

in.nextLine(); 
5

這應該修復它...

Scanner in=new Scanner(System.in); 
    int rounds = 0; 
    while (rounds < 1 || rounds > 3) { 
     System.out.print("How many rounds? "); 
     if (in.hasNextInt()) { 
      rounds = in.nextInt(); 
     } else { 
      System.out.println("Invalid input. Please try again."); 
      in.next(); // -->important 
      System.out.println(); 
     } 
     // Clear buffer 
    } 
    System.out.print(rounds+" rounds."); 
5

使用以下命令:

in.nextLine(); 

右後

System.out.println("Invalid input. Please Try Again."); 
System.out.println(); 

或之後的大括號(您的評論是關於它的地方)。

該命令將掃描器推進到下一行(當從文件或字符串中讀取時,這隻讀取下一行),因此在本例中基本上將其刷新。它清除緩衝區併爲掃描儀準備新的輸入。當用戶輸入一個無效的輸入時,它可以,最好用於清除當前的緩衝區(例如,當詢問一個數字時,它是一個字母)。該方法的

文檔可以在這裏找到: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()

希望這有助於!

0

試試這個代碼:

Scanner in=new Scanner(System.in); 
    int rounds = 0; 
    while (rounds < 1 || rounds > 3) { 
     System.out.print("How many rounds? "); 
     if (in.hasNextInt()) { 
      rounds = in.nextInt(); 
     } else { 
      in.NextLine(); // to clear Scanner 
     //You can throw new InputMismatchException(); 
      System.out.println("Invalid input. Please try again."); 
      System.out.println(); 
     } 

    } 
    System.out.print(rounds+" rounds."); 
0

一種解決方案是將do-while循環裏面的銀行對象主要讓新的銀行對象每次創建。

但是這提出了某些問題,即帳戶數組和計數變量。你可以通過將它們放在bankUser中來解決這個問題。

static int count = 0; //don't forget to make count global and initialize 
public static void main(String[] args) { 


    int Choice; 
    //place the Acct array inside bankUser main or declare 
    //as global 
    bankAcct myAcct[] = new bankAcct[3]; 

    do 
    { //Place the bank object inside the do-while 
    Bank myBank = new Bank(count, myAcct); //pass the variables to the 
              //new Bank object 
    dispMenu(); 

    Choice = getChoice(); 

    proChoice(Choice, myBank); 

    } 
    while (Choice !=0); 
    } 

不要忘了讓計全球,因爲它需要能夠被傳遞到主的對象,並通過在proChoice方法的switch-case openAcct遞增。

case 1: myBank.openAcct(); 
    count++; 
    break; 

最後,你可以通過使用對象在銀行類的構造函數的myAcct陣列和計數變量。

public Bank(int count, bankAcct myAcct[]) { 
this.count = count; 
this.myAcct = myAcct; 

}