Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class NumberTwo {
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         System.out.println("Please enter filename");
  8.         String inputFileName = scan.next();
  9.         scan.close();
  10.         File inputFile = new File(inputFileName);
  11.         try {
  12.             Scanner in = new Scanner(inputFile);
  13.             String line = "";
  14.             while(in.hasNextLine()) {
  15.                 line = in.nextLine();
  16.                 String[] values = line.split(",");
  17.                 if(line.isEmpty()) {
  18.                     continue;
  19.                 }
  20.                 double[] intvalues = new double[values.length];
  21.                 for (int i = 0; i < values.length; i++) {
  22.                     intvalues[i] = Double.parseDouble(values[i]);
  23.                 }
  24.                 double median = 0, min = intvalues[0], max = intvalues[0];
  25.                 for(double a : intvalues) {
  26.                     if(min > a) {
  27.                         min = a;
  28.                     }
  29.                     if(max < a) {
  30.                         max = a;
  31.                     }
  32.                 }
  33.                 if(intvalues.length % 2  == 0) {
  34.                     median = intvalues[intvalues.length / 2];
  35.                     median += intvalues[intvalues.length / 2 - 1];
  36.                     median /= 2;
  37.                 }else {
  38.                     median = intvalues[intvalues.length / 2];
  39.                 }
  40.                 PrintWriter out = new PrintWriter(new FileWriter("output", true));
  41.                 out.print("Min = " + min + " Max = " + max + " Median = " + median +"\n");
  42.                 out.close();
  43.             }
  44.  
  45.         }
  46.         catch (FileNotFoundException e) {
  47.             System.out.println(e.getMessage());
  48.         } catch (IOException e) {
  49.             System.out.println(e.getMessage());
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement