2016-03-10 83 views
0

这是我到目前为止。我不知道如何在屏幕上打印最低和最高的数字。任何帮助将不胜感激。谢谢。我需要打开一个文本文件,其中包含整数,我需要打印出文件中最小和最小的数字

BufferedReader openFile; 
    try{ 
     openFile = new BufferedReader(new FileReader("LABEX10.txt")); 
    } 
    catch(FileNotFoundException e){ 
     System.out.println("Could not open file LABEX10.txt"); 
     System.exit(0); 
    }catch(IOException ex){ 
     System.err.println(ex); 
    } 
} 

}

+0

对它进行排序并取最终值? – logger

+0

解决方案可以在这里找到http://stackoverflow.com/questions/15534024/how-to-determine-the-max-and-min-values-read-in-from-a-text-file-in-java – berlinguyinca

+0

那是我不知道该怎么做的。我如何分类? –

回答

2

使用Java 8:

final Path path = Paths.get("LABEX10.txt"); 
try (Stream<String> lines = Files.lines(path)) { 
    final LongSummaryStatistics summary = lines 
      .mapToLong(Long::parseLong) 
      .summaryStatistics(); 
    System.out.printf("range [%d .. %d]%n", summary.getMin(), summary.getMax()); 
} 

这种方法比proposed duplicate更现代。

相关问题