2015-09-04 48 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()解决了我的问题。