Fakiye Oladipupo
Fakiye Oladipupo

Reputation: 1

I need to open a text file that contains integers and i need to print out the smallest and lowest numbers in the file

Here is what i've got so far. I dont't know how to print the lowest and highest number to the screen. Any help would be appreciated.Thanks.

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);
    }
}

}

Upvotes: 0

Views: 56

Answers (1)

Boris the Spider
Boris the Spider

Reputation: 61168

Using 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());
}

This approach is more modern than the proposed duplicate.

Upvotes: 2

Related Questions