I am having trouble reading the entire file and placing it into the array. It keeps leaving out the last 2 numbers in my txt file. What am I doing wrong in my createArr() method that is causing this. Also, my method avgTimeTemp() is not producing any output other than the first line. I included my text file contents below and output.
Text File Contents
98 95 95 102
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
OUTPUT:
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:
try