You may assume that a person eats at least once a day. That is, each row will ha
ID: 3747346 • Letter: Y
Question
You may assume that a person eats at least once a day. That is, each row will have at least one number Example input file: Your program must read the input from a file named input2.txt 200 1000 450 845 1200 800 800 400 1500 1800 200 500 1000 700 1400 170 675 400 100 400 300 Use the concept of 2 dimensional Ragged Array to solve the second part of the assignment. In the second part, the client is only interested in the following information now the average number of calories consumed each day the average number of calories consumed in every meal of the week. For the example input file above the list of averages for Meals 1 to 5 is: 532.1428571, 1024.166667, 817.5, 466.6666667, and 300 . .Explanation / Answer
import java.io.*;
import java.util.*;
public class sumfile {
public static void main(String args[]) {
File f = new File(args[0]);
float weakavg[]=new float[7];
int max=0;
/*The below code calculates the average calories consumed in every day */
try{
BufferedReader br;
br=new BufferedReader(new FileReader(f));
String line=null;
int day = 1;
while((line=br.readLine())!=null){
int sum=0;
float avg=0;
int i=0;
String[] temp=line.split(" ");
if(max<temp.length)
max=temp.length;
while(i<temp.length){
sum +=Integer.parseInt(temp[i]);
i++;
}
avg = (float)sum/temp.length;
System.out.println("Average Number of calories consumed on Day" + day++ + " = " + avg);
}
}catch(Exception e)
{
System.out.println("Error");
}
int meal=0;
/* The below portion code calculates the average calories calculated in every meal of the week */
BufferedReader br2;
while(max>0)
{
try{
br2=new BufferedReader(new FileReader(f));
max--;
String line2=null;
float avgm=0;
int summ=0;
int nmeals=0;
String[] tempm=null;
while((line2=br2.readLine())!=null){
// int summ=0;
//float avgm=0;
int im=0;
//int nmeals=0;
tempm=line2.split(" ");
if((tempm.length-meal)>0){
summ +=Integer.parseInt(tempm[meal]);
nmeals++;
}
}
avgm = (float)summ/nmeals;
System.out.println("Average Number of calories consumed on Meal" + ++meal + " in the entire week = " + avgm);
}catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.