Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. package com.company;
  2. import java.io.*;
  3. import java.util.*;
  4. public class Main {
  5.  
  6.     public static void main(String[] args) throws IOException {
  7.         int[] arr = new int[6];
  8.         BufferedReader reader = null;
  9.         int counter = 0;
  10.         try {
  11.             reader = new BufferedReader(new FileReader("C:\\Users\\gzero\\OneDrive\\Desktop\\lab stuff.txt"));
  12.             String text = null;
  13.             while ((text = reader.readLine()) !=null)
  14.             {
  15.                 arr[counter]=Integer.parseInt(text);
  16.                 counter++;
  17.             }
  18.         } catch (FileNotFoundException e) {
  19.             e.printStackTrace();
  20.             System.out.println("lab stuff.txt doesn't exist");
  21.         } catch (IOException e) {
  22.             e.printStackTrace();
  23.         }
  24.         sort(arr);
  25.  
  26.         System.out.println(Arrays.toString(arr));
  27.  
  28.         File output = new File("LabFinal.txt");
  29.         output.createNewFile();
  30.         FileWriter writer = new FileWriter(output);
  31.         writer.write(Arrays.toString(arr)+ "\n Average rainfall is " +
  32.                 average(arr)+ "\n The minimum is "+minimum(arr));
  33.  
  34.  
  35.         reader.close();
  36.         writer.close();
  37.     }
  38.     public static void sort(int a[])
  39.     {
  40.         int n = a.length;
  41.         for(int i = 0; i< n-1; i++)
  42.         {
  43.             int minIDX = i;
  44.             for(int j = i+1; j<n; j++)
  45.             {
  46.                 if(a[j]> a[minIDX])
  47.                     minIDX=j;
  48.             }
  49.             int temp = a[minIDX];
  50.             a[minIDX]=a[i];
  51.             a[i]= temp;
  52.         }
  53.     }
  54.     public static int average(int a[])
  55.     {
  56.         int total=0;
  57.         for(int i =0; i>a.length;i++)
  58.         {
  59.             total = a[i]+total;
  60.  
  61.         }
  62.         return total/6;
  63.     }
  64.     public static int minimum(int a[])
  65.     {
  66.         int min= a[0];
  67.         for(int i= 0; i>a.length;i++)
  68.         {
  69.             if(min >a[i])
  70.             {
  71.                 min = a[i];
  72.             }
  73.         }
  74.         return min;
  75.     }
  76.  
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement