Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1.     public static void readFile(int lowerBound, int upperBound, String filename, int howMany, int numberOfLines) throws IOException
  2.     {
  3.         // Open the file
  4.         File file = new File(filename);
  5.         Scanner inputFile = new Scanner(file);
  6.  
  7.         int sum = 0;
  8.        
  9.         int middleGround = upperBound - ( (upperBound - lowerBound) / 2 );
  10.         int lowestNumber = middleGround;
  11.         int greatestNumber = middleGround;
  12.  
  13.         while (inputFile.hasNext())
  14.         {
  15.             int line = inputFile.nextInt();
  16.             sum += line;
  17.  
  18.             if ( line < lowestNumber )
  19.             {
  20.                 lowestNumber = line;
  21.             }
  22.  
  23.             if ( line > greatestNumber )
  24.             {
  25.                 greatestNumber = line;
  26.             }
  27.         }
  28.  
  29.         double average = (double)sum / numberOfLines;
  30.  
  31.         // Print the lines
  32.         System.out.printf("\n\nThere were %d lines in the file.\n" +
  33.                 "The largest number in the file is %d.\n" +
  34.                 "The smallest number in the file is %d.\n"+
  35.                 "The sum of all the numbers in the file is %d.\n"+
  36.                 "The average of all the numbers in the file is %1f.\n", +
  37.                 numberOfLines,greatestNumber,lowestNumber,sum,average );
  38.  
  39.         // Close the file
  40.         inputFile.close();
  41.  
  42.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement