Kevin
Kevin

Reputation: 17

Min Max and Avg from an input text file

Been working on this homework task for quite a while now and cant seem to get the answer, some feedback on my code would be appreciated. I have an input text file that has the following in it

Min: 1,2,3,5,6
     Max: 1,2,3,5,6
Avg: 1,2,3,5,6
    P90: 1,2,3,4,5,6,7,8,9,10
Sum: 1,2,3,5,6
    P70: 1,2,3

my task is to read the input text file and create an output text file that reads as follows:

The min of [1,2,3,5,6] is 1.
    The max of [1,2,3,5,6] is 6.
The avg of [1,2,3,5,6] is 3.4.
    The 90th percentile of [1,2,3,4,5,6,7,8,9,10] is 9.
The sum of [1,2,3,5,6] is 17.
    The 70th percentile of [1,2,3] is 2.

So far I am just trying the min max and avg, I can read the file and I can output what I read in. I dont know how to find min max avg etc and output those values. seems to be because my input is a string and I need to only read the int values. Here is my code

import java.io.File;
import java.util.Formatter;
import java.util.Scanner;

public class ExternalData {

public static void main(String[] args) {

    try {
        System.out.print("Enter the file name with extension : ");

        Scanner input = new Scanner(System.in);

        File file = new File(input.nextLine());

        input = new Scanner(file);

        while (input.hasNextLine()) {
            int min = input.nextInt();
            while(input.hasNextInt());
            int num = input.nextInt();
            if(num < min) {
                min = num;
            }

            while (input.hasNextLine()) {
                int max = input.nextInt();
                while(input.hasNextInt());
                int num2 = input.nextInt();
                if(num2 > min) {
                    min = num;

            String avg = input.nextLine();




            Formatter f = new Formatter("C:\\Users\\Kevin\\Dropbox\\Kevin 
            Carter-8042\\Intro to Soft Eng\\Task 12\\outputTest.txt");

            // print the formatted strings to the file
            f.format("The min of [1,2,3,4,5,6] is " + min);

            f.format("\n\nThe max of [1,2,3,4,5,6] is " + max);

            f.format("\n\nThe avg of [1,2,3,4,5,6] is " + avg);
            f.close();    
            }     

            } catch (Exception ex) {
              ex.printStackTrace();
            }

          }

       }

Upvotes: 1

Views: 1388

Answers (1)

Dora
Dora

Reputation: 191

package TrialPrograms;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

public class Test {

    public static void main(String[] args) throws IOException {



        FileInputStream fi = new FileInputStream("F:\\Test\\file.txt");
        FileOutputStream fo = new FileOutputStream("F:\\Test\\output.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fi));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fo));

        String strLine;
        while ((strLine = br.readLine()) != null)   {

              String[] arr = strLine.split(" ");
              String[] nos = arr[1].split(",");

              Set<Integer> set = new HashSet<Integer>();  
              for(int i = 0; i<nos.length; i++){
                  int no = Integer.parseInt(nos[i]);
                       set.add(no); 
                    }
              TreeSet<Integer> sortedSet = new TreeSet<Integer>(set); 

              switch(arr[0]) {

              case "Min:":
                  String msg1="The Min of [" +arr[1]+ "] is " +(Integer)sortedSet.first();
                  bw.write(msg1);
                  bw.newLine();

                  break;

              case "Max:":
                  String msg2="The Max of [" +arr[1]+ "] is " +(Integer)sortedSet.last();
                  bw.write(msg2);
                  bw.newLine();
                  break;

              case "Avg:":
                  Object[] noarray = sortedSet.toArray();
                  int noarraysize = noarray.length-1;
                  int sum=0;
                  for(int i=0;i<=noarraysize;i++) {

                      int no=Integer.valueOf(noarray[i].toString());
                      sum = sum + no;
                      if(i==noarraysize) {
                          String msg3="The Avg of [" +arr[1]+ "] is  " +(double)sum/noarray.length;
                          bw.write(msg3);
                          bw.newLine();
                                                  }
                  }
                  break;

              case "Sum:":
                  Object[] noarray1 = sortedSet.toArray();
                  int noarraysize1 = noarray1.length-1;
                  int sum1=0;
                  for(int i=0;i<=noarraysize1;i++) {
                      int no=Integer.valueOf(noarray1[i].toString());
                      sum1 = sum1 + no;
                      if(i==noarraysize1) {
                          String msg4="The Sum of [" +arr[1]+ "] is  " +sum1;
                          bw.write(msg4);
                          bw.newLine();
                                                  }
                  }
                  break;

            }

    }
        br.close();
        bw.close();

    }
}

Upvotes: 1

Related Questions