// Miya Natsuhara // 07-29-2019 // CSE142 // TA: Grace Hopper // Reads an input file and reports the real numbers read in (skipping and reporting non-number // tokens) and prints out the minimum and maximum values read. import java.io.*; import java.util.*; public class FindMinAndMax { public static void main(String[] args) throws FileNotFoundException { File f = new File("data.txt"); Scanner input = new Scanner(f); // if had guarantees about range of numbers, could also: // initialize min to biggest possible number // initialize max to smallest possible number double firstVal = input.nextDouble(); System.out.println("read number: " + firstVal); double min = firstVal; double max = firstVal; while (input.hasNext()) { if (input.hasNextDouble()) { double nextVal = input.nextDouble(); System.out.println("read number: " + nextVal); min = Math.min(min, nextVal); max = Math.max(max, nextVal); } else { System.out.println("skipping bad token: " + input.next()); } } System.out.println("minimum was " + min + ", maximum was " + max); } }