/************************************************************************* /*MaxInt.java * Author: K Sree Harsha * Course: CSE11 Fall 2013 * Assignment: Program #6 * * Date Written: 16 November 2013 * Last Modified:16 November 2013 * * Description: * Reads a file of Integers and prints out the Maximium integer in the * file and the number of times the maximium occurs. * * Build: javac -classpath '*':'.' MaxInt.java * Run: java MaxInt * * public methods defined: * main * * Public class variables: * None * ***************************************************************************/ import java.util.Scanner; import java.io.*; public class MaxInt { public static void main (String args[]) { int nelems,max=0,ntimes=0,temp=0; String fname = args[0]; Scanner scan; try { // Open the file, find out how many lines scan = new Scanner(new File(fname)); scan.useDelimiter(System.getProperty("line.separator")); nelems = 0; if(scan.hasNext()) max=scan.nextInt(); while (scan.hasNext()) { temp=scan.nextInt(); if(temp>max) { max=temp; ntimes=0; } if(max==temp) ntimes++; nelems++; } if(nelems>0) System.out.format("%d : %d \n", max, ntimes); } catch (FileNotFoundException e) { e.printStackTrace(); } } }