-Can anyone help me using the scanner class? How do you read a file from the com
ID: 3812134 • Letter: #
Question
-Can anyone help me using the scanner class? How do you read a file from the command line arguments?
My neighbor’s daughter has been working on a science project where she has collected the high temperature for each day of an entire month. Now she needs to display information regarding heat waves by highlighting those days that might be considered part of a heat wave. You are to write a program to help.
All of the data has been stored in a text file. Because there are several months of data, your program will read the file name using command line arguments.
The information is stored as follows:
• The first line of the file contains the name of the month that the data was collected.
• The remaining lines will contain one value per line which represents the high temperature for that day.
• All values are integers.
You are to write a Java program which first reads in the month name and determines the number of days in the month the data was collected. You must use a separate method for this procedure and the method header must be,
private static int numDaysInMonth(String month)
Next, read in the temperatures. Calculate and print the average temperature (two decimal places) for the month. For each day of the month, print the day number and the temperature that was read from the original file. For every day that is in part of a set of 3 or more consecutive days above the average, display a “+” beside the temperature. Numbers should line up so that the ones digits are all in the same column. Your program must use at least one array. See the sample output below.
Save your program in a file called TempAnalysis.java.
Output for the sample file:
The average temperature for August was 91.97
1. 85
2. 87
3. 92+
4. 95+
5. 97+
6. 98+
7. 90
8. 84
9. 87
10. 93+
11. 95+
12. 96+
13. 96+
14. 94+
15. 90
16. 91
17. 93+
18. 96+
19. 94+
20. 95+
21. 96+
22. 94+
23. 95+
24. 89
25. 89
26. 89
27. 87
28. 97
29. 88
30. 87
Explanation / Answer
// TempAnalysis.java
import java.util.*;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
class TempAnalysis
{
static String filename;
static BufferedReader br=null;
private static int numDaysInMonth(String month)
{int count=0;
try
{
String line2=br.readLine();
line2=br.readLine();
count=1;
while(br.readLine()!=null)
{
count++;
}}
catch(Exception e)
{
System.out.println(e);
}
return count;
}
public static void main(String[] args)
{
Scanner s=new Scanner(System.in) ;
System.out.println("Enter FileName");
BufferedReader br1=null;
filename=s.next();
try{
// create new file input stream
br = new BufferedReader(new FileReader(filename));
br1 = new BufferedReader(new FileReader(filename));
// read till the end of the file
String line1=br1.readLine();
int days=numDaysInMonth(line1);
System.out.println("Data will be collected for "+days+" no of days");
int array[]=new int[days];
int i=0;
String line=br1.readLine();
while((line)!=null)
{
array[i++]=Integer.parseInt(line);
line=br1.readLine();
}
Double total=0.0;
for(int j=0;j<array.length;j++)
{
total=total+array[j] ;
}
Double average=total/array.length;
System.out.println("Average Temperature for "+line1+" was "+average) ;
for(int j=0;j<array.length;j++)
{
if(array[j]>average)
{
System.out.println(array[j]+"+") ;
}
else
System.out.println(array[j]) ;
}
}
catch(Exception ex){
// if any error occurs
ex.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.