2015-09-04 12 views
0

我正在嘗試創建一個簡單的應用程序,它接收一定數量的名稱並從中創建錦標賽括號。現在我被卡在每場比賽中創造比賽和參賽者的數量。現在,我想創建一個故障安全系統,它會警告用戶他們想要在比賽中的人數不會與總人數相等。我請他們簡單地輸入'Y'或'N'來指示這一點。問題是程序沒有給他們輸入響應的機會,並自動吐出NoSuchElementFound異常:找不到行。我的掃描儀沒有下一行,導致NoSuchElementFound異常

這是一種全新的掃描儀,用於採取此響應。我嘗試使用.hasNext來查看是否沒有下一行,並且得到的結果是錯誤的。從本質上講,我已經得到了所有的參賽者(即正常工作一個單獨的方法)後,這是控制檯:有多少選手你想每競爭5

總參賽人數比賽?

警告:選擇這個數目意味着將不會有相同數量的每個匹配參賽者。

提醒參賽者的總數爲:5

如果你想繼續,輸入 'Y'。如果您想爲每場比賽輸入不同數量的參賽者,請輸入'N'。

校驗值:假

如果有幫助,這是麻煩的代碼:

String answer = "yes"; 

    Scanner scan = new Scanner(System.in);  
    float roundedMatchCount = 0; 

    float matchCount = 0; 
    entrant victor = new entrant(); 

    float matchMembers; 

    Scanner scan = new Scanner(System.in); 

    System.out.println("How many contestants do you want to compete per match?"); 
    matchMembers = scan.nextInt(); 



    matchCount = input.size()/matchMembers; 



    scan.close(); 

    Scanner s = new Scanner(System.in); 


    if (input.size() % matchMembers !=0) 
     //if the number of contestants is not divisble by the number 
     //of participants in a match 
    { 
     System.out.println("Warning: Choosing this number means that there will not be the same number of contestants in each match."); 
     System.out.println("Reminder that the total number of contestants is: " 
       +input.size()); 
     //begin while 


      System.out.println("If you want to proceed, enter 'Y'. If you want to enter a different number of contestants per match, enter 'N'."); 

      boolean check = s.hasNextLine(); 

      System.out.println("check value: " + check); 

      answer = s.nextLine(); 
      //WHAT 

      if (answer.contains("N")) 
      { 
       //ask for a new number 

      } 

      else if (answer.contains("Y")) 
      { 
       //round the number of matches up or down depending on user input 
      } 

      else 
      { 
       System.out.println("Error: Invalid response."); 

      } 


    } 

請注意,輸入的是,被傳遞到與以前的這種方法的ArrayList。我知道正確的條目數量在裏面,因爲在之前的測試中,我已經打印了它的內容和尺寸。

+0

嘗試擺脫布爾檢查行和後面的打印語句。 'answer = s.nextLine()'應該是你需要的。 – Zarwan

+0

這就是我在執行.hasNextLine()檢查之前所做的事情。同樣的錯誤,.hasNextLine()被添加爲調試過程的一部分。 –

+0

好的,但你仍然應該擺脫它。同樣擺脫'scan.close()';那也是關閉你的輸入流'System.in'。 – Zarwan

回答

0

由於KDM有幫助指出,我的問題是由我如何關閉上一個掃描儀造成的。通過scan.close()解決了我的問題。