简体   繁体   中英

Min Max Average Data input

Still struggling with this. Need to find the min, max and average of input data and print it to an external data file. Can anyone spot problems in my code. It doesn't print out anything.

It needs to read a file that has the following

min:1,2,3,4,5,6
max:1,2,3,4,5,6
avg:1,2,3,4,5,6

but needs to print it out in the following format

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.




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();

}

}

If I add a print statement outside of the code body it works but none of the messages that I input will print externally to a text file.

This line looks to be your first problem:

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

arr only contains a single element so you access outside the arrays bounds (remember arrays are 0 indexed, so the first element is arr[0]).

You also have another issue here:

int no = Integer.parseInt(nos[i]);

Before you try to parse an int from your input you need to consume the min: part of your input.

made a few more changes that make this work with your the input as you provided: 进行了一些其他更改,使之可以与您提供的输入一起使用:

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

        FileInputStream fi = new FileInputStream("file.txt");
        FileOutputStream fo = new FileOutputStream("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();

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM