next up previous
Next: Comparison between loop for Up: Unit 11 Previous: Example: number of occurrences

Example: maximum of positive integers read from a file

Recursive characterization of the operation of finding the maximum among a set of values read from a file that contains positive integers:

Implementation: we access the file through a BufferedReader, and we assume that each integer is written on a separate line.

public static int maximum(BufferedReader br) throws IOException {
  String s = br.readLine();
  if (s == null)
    return 0;
  else {
    int i = Integer.parseInt(s);
    int m = maximum(br);
    return (m > i)? m : i;
  }
}


next up previous
Next: Comparison between loop for Up: Unit 11 Previous: Example: number of occurrences