任务让我发疯

时间:2018-01-28 18:51:04

标签: java external

现在已经有很长一段时间没有完成这项功课了,似乎无法得到答案,我的代码会得到一些反馈意见。我有一个输入文本文件,其中包含以下内容

 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

我的任务是读取输入文本文件并创建一个输出文本文件,内容如下:

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.

到目前为止,我只是尝试min max和avg,我可以读取文件,我可以输出我读到的内容。我不知道如何找到min max avg等并输出这些值。似乎是因为我的输入是一个字符串,我只需要读取int值。无论我做什么,它都会运行,但不会在外部文本文件中打印任何内容。

public class ExternalData {

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



    FileInputStream fi = new FileInputStream("C:\\Users\\Kevin\\Dropbox\\Kevin Carter-8042\\Intro to Soft Eng\\Task 12\\input.txt");
    FileOutputStream fo = new FileOutputStream("C:\\Users\\Kevin\\Dropbox\\Kevin Carter-8042\\Intro to Soft Eng\\Task 12\\outputTest.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fi));
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fo));

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

        System.out.println(strLine);

        String[] arr = strLine.split("");
        System.out.println(Arrays.toString(arr));
        String[] nos = arr[4].split(",");
        System.out.println(Arrays.toString(nos));

        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[2] + "] is " + (Integer) sortedSet.first();
                System.out.println(msg1);
                bw.write(msg1);
                bw.newLine();

                break;

            case "Max:":
                String msg2 = "The Max of [" + arr[0] + "] is " + (Integer) sortedSet.last();
                System.out.println(msg2);
                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[0] + "] is  " + (double) sum / noarray.length;
                        System.out.println(msg3);
                        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[0] + "] is  " + sum1;
                        System.out.println(msg4);
                        bw.write(msg4);
                        bw.newLine();
                    }
                }
                break;

        }

    }

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

}
}

1 个答案:

答案 0 :(得分:0)

split("")笨重,尝试使用它的结果是有问题的。为了简化这个问题,首先用冒号分割输入,这将有助于确定操作和整数列表(nos)

    String[] resultOfColanSplit = strLine.split(":");
    String operation = resultOfColanSplit [0];
    String[] nos = resultOfColanSplit [1].split(",");

现在您将拥有所需的值(根据您的原始逻辑,您的nos []数组不正确。)

然后在您的switch语句中,使用&#39; operation&#39;而不是arr [0],并删除&#34;:&#34; (它不再是操作字符串的一部分)。

我发现了另一个与解析int相关的问题,并发现替换空白有帮助:

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