Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void readFile(int lowerBound, int upperBound, String filename, int howMany, int numberOfLines) throws IOException
- {
- // Open the file
- File file = new File(filename);
- Scanner inputFile = new Scanner(file);
- int sum = 0;
- int middleGround = upperBound - ( (upperBound - lowerBound) / 2 );
- int lowestNumber = middleGround;
- int greatestNumber = middleGround;
- while (inputFile.hasNext())
- {
- int line = inputFile.nextInt();
- sum += line;
- if ( line < lowestNumber )
- {
- lowestNumber = line;
- }
- if ( line > greatestNumber )
- {
- greatestNumber = line;
- }
- }
- double average = (double)sum / numberOfLines;
- // Print the lines
- System.out.printf("\n\nThere were %d lines in the file.\n" +
- "The largest number in the file is %d.\n" +
- "The smallest number in the file is %d.\n"+
- "The sum of all the numbers in the file is %d.\n"+
- "The average of all the numbers in the file is %1f.\n", +
- numberOfLines,greatestNumber,lowestNumber,sum,average );
- // Close the file
- inputFile.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement