Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将参差不齐的文件读入2d数组-打印-并查找列的平均值_Java_Arrays_File_Ragged - Fatal编程技术网

Java 将参差不齐的文件读入2d数组-打印-并查找列的平均值

Java 将参差不齐的文件读入2d数组-打印-并查找列的平均值,java,arrays,file,ragged,Java,Arrays,File,Ragged,读取整个文件并将其放入数组时遇到问题。它总是漏掉我的txt文件中的最后2个数字。我在createArr()方法中做错了什么导致了这种情况。 另外,我的方法avgtimetem()除了第一行之外,没有生成任何输出。我在下面包括了我的文本文件内容和输出 文本文件内容 9895102 99 96 99.5 97 100 97.5 97.5 101 98.5 98 101 100 102.5 99.5 99.5 95 96.5 102 97.5 import java.io.File; import j

读取整个文件并将其放入数组时遇到问题。它总是漏掉我的txt文件中的最后2个数字。我在createArr()方法中做错了什么导致了这种情况。 另外,我的方法avgtimetem()除了第一行之外,没有生成任何输出。我在下面包括了我的文本文件内容和输出

文本文件内容

9895102

99 96

99.5 97

100 97.5 97.5

101 98.5 98 101 100 102.5

99.5

99.5 95 96.5 102 97.5

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

public class Temperature2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try {
            //create scanner and read file
            File input = new File("lab2input2.txt");
            Scanner klg = new Scanner(input);

            //Variable declarations
            double [][] arr = new double [7][];

            System.out.println("Hello! Thank you for saving lives! ");
            System.out.println(" ");
            System.out.println("Please fill in the data for your patient:");
            System.out.println("Patient Name:");
            System.out.println("Date(Reporting week): ");
            System.out.println(" ");
            System.out.println("This Weeks temperature data is incomplete and fragmented... but thats ok! We still have your averages:  ");
            System.out.println("Your Patients Weekly Temperature Record for Time Classes T1 - T6 are as follows:");
            System.out.println("     NOTE: Week starts MONDAY(Day1) and ends SUNDAY(Day7)");
            System.out.println(" ");


            //Method Calls
            createArr(klg, arr);
            printArr(arr);
            avgDailyTemp(arr);
            avgTimeTemp(arr);

            klg.close();
        }   //End of Try
        catch (FileNotFoundException e) {
            System.out.println("The file was not found."); 
        }   
    }   //End of Main

    public static void createArr(Scanner klg, double [][] arr) {
        int i, j;

        for(i = 0; i < arr.length; i++) {
            if(arr.length != 7 && arr != null){
                System.out.print("There is not data for 7 days in the file you inputed. Please check file contents.");  
            }
            arr[i] = new double[i]; //I Feel I have a problem in this line
            for(j = 0; j< arr[i].length; j++) {
                arr[i][j] = klg.nextDouble();
            }
        }
    }
    //Print array (TESTING PURPOSES)
    public static void printArr(double [][] arr) {
        System.out.println("The file contains the following input values: ");       
        for (int i = 0; i< arr.length; i++){
            for(int j = 0; j < arr[i].length; j++) {
                System.out.println(arr[i][j] + " ");    
            }
        }
    }
    //The average temperature of a patient each day from Monday to Sunday
    public static void avgDailyTemp(double [][] arr) {
        double sum, avg;
        System.out.println("Average temperature each day this week: ");
        for(int i = 0; i < arr.length; i++) {
            sum = 0;
            for(int j = 0; j<arr[i].length; j++){
                sum = sum + arr[i][j];
            }
            avg = sum/6;
            System.out.println("Day " + (i+1) + " " + avg);
        }
        System.out.println(" ");
    }
    //NOT WORKING - The average temperature of patient at Time Classes T1-T6
    public static void avgTimeTemp(double [][] arr) {
        double sum; 
        System.out.println("Average temperature for each Time Class: ");

        for (int j = 0; j < arr[j].length; j++){
            double [] avgArr = new double [j];
            sum = 0; 
            for (int i = 0; i < arr.length; i++){ 
                sum = sum + arr[i][j];
            }
            avgArr[j] =sum/7;
            System.out.println("Time Class T" + (j+1) + ": " + avgArr[j]);
        }
        System.out.println(" ");
    }
}   //End of Class
试一试

试一试


好的,我明白了,我必须拆分它来得到每列的每个值。这很有效!我有我需要的所有数字。非常感谢。我已经为此工作了好几天了!我会的,我是stackoverflow的新手,我知道你能做到。谢谢好的,我明白了,我必须拆分它来得到每列的每个值。这很有效!我有我需要的所有数字。非常感谢。我已经为此工作了好几天了!我会的,我是stackoverflow的新手,我知道你能做到。谢谢
Hello! Thank you for saving lives! 

Please fill in the data for your patient:
Patient Name:
Date(Reporting week): 

This Weeks temperature data is incomplete and fragmented... but thats ok! We still have your averages:  
Your Patients Weekly Temperature Record for Time Classes T1 - T6 are as follows:
     NOTE: Week starts MONDAY(Day1) and ends SUNDAY(Day7)

The file contains the following input values: 

98.0 

95.0 

95.0 

102.0 

99.0 

96.0 

99.5 

97.0 

100.0 

97.5 

97.5 

101.0 

98.5 

98.0 

101.0 

100.0 

102.5 

99.5 

99.5 

95.0 

96.5 

Average temperature each day this week: 

Day 1 0.0

Day 2 16.333333333333332

Day 3 31.666666666666668

Day 4 49.5

Day 5 65.66666666666667

Day 6 82.66666666666667

Day 7 98.83333333333333

Average temperature for each Time Class: 
public static void createArr(Scanner klg, double [][] arr) {

    int i = 0;


    while (klg.hasNextLine()) {


        String line = klg.nextLine();
        String [] vals = line.split(" "); 

        arr[i] = new double [vals.length];
        int j = 0;

        for (String val : vals) {
            arr[i][j++] = Double.parseDouble(val);
        }

        i++;
    }

}