Advertisement
Aldin-SXR

MaxInteger

Feb 27th, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. package ds.intro;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.Scanner;
  8.  
  9. public class MaxInteger {
  10.    
  11.     /* Read the integer list from a file in the given path */
  12.     public static int[] readNumbers(String path) throws FileNotFoundException {
  13.         int[] numbers = new int[100];
  14.         Scanner scanner = new Scanner(new File(path));
  15.         int i = 0; // counter
  16.         while (scanner.hasNextLine()) {
  17.             numbers[i] = Integer.parseInt(scanner.nextLine());
  18.             i++;
  19.         }
  20.         scanner.close();
  21.         return numbers;
  22.     }
  23.    
  24.     /* Find the maximum integer */
  25.     public static int findMax(int[] numbers) {
  26.         int max = numbers[0];
  27.         for (int i = 1; i < numbers.length; i++) {
  28.             if (numbers[i] > max) {
  29.                 max = numbers[i];
  30.             }
  31.         }
  32.         return max;
  33.     }
  34.    
  35.     /* Write the maximum number to a file */
  36.     public static void writeToFile(String path, int maxNumber) throws IOException {
  37.         File file = new File(path);
  38.         FileWriter fw = new FileWriter(file);
  39.         fw.write(Integer.toString(maxNumber));
  40.         fw.close();
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement