// A program to read in any number of real numbers from a file and find // the minimum and maximum values read. Can handle any number of values // in the file (including zero) as well as non-numbers interspersed. // Assumes the first token of the file is a real number. // // Note the use of .hasNext() vs. .hasNextDouble() and .next() vs. nextDouble() // to check for/read any token or specifically a double. import java.util.*; import java.io.*; public class FindMinAndMax3 { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("numbers3.txt")); if (input.hasNextDouble()) { double number = input.nextDouble(); System.out.println("Read number: " + number); double min = number; double max = number; int count = 1; while (input.hasNext()) { if (input.hasNextDouble()) { // found a number number = input.nextDouble(); count++; System.out.println("Read number: " + number); if (number > max) { max = number; } if (number < min) { min = number; } } else { // found not a number String token = input.next(); System.out.println(" Skipping non-number: " + token); } } System.out.println(); System.out.println("Read " + count + " numbers."); System.out.println("Maximum value: " + max); System.out.println("Minimum value: " + min); } } }