我正在尝试编写一个Java程序,其中计算从输入文件参加聚会的人数。该文件将始终采用这种格式,例如:用户将不知道输入格式中有多少行。该程序将返回有多少人参加聚会,有多少人没有参加聚会。以下是我当前的代码:我希望我的输出像这个例子一样。但是,我收到此错误。有什么办法可以解决此问题并改进代码以获取所需的格式?
2 回答
![?](http://img1.sycdn.imooc.com/545865620001c45402760276-100-100.jpg)
慕容森
TA贡献1853条经验 获得超18个赞
您正在使用的混合物Scanner
和BufferedInputStream
。只需选择一个并坚持下去即可。还是更好-使用现代的Files#lines
:
Map<Boolean, Long> counts = Files.lines(Paths.get("partyResponses.txt")) .collect(Collectors.partitioningBy(s -> s.endsWith("yes"), Collectors.counting()));System.out.printf("%d happy friends coming to the July 4th party!%n", counts.get(true);System.out.printf("%d sad friends can't make it%n", counts.get(false);
添加回答
举报
0/150
提交
取消