Write a Java program that inputs 7 double values from a file dja.txt that repres
ID: 3691194 • Letter: W
Question
Write a Java program that inputs 7 double values from a file dja.txt that represent the Dow Jones Average for 7 days. Your program should output the lowest value for those 7 days and the number of the day on which the lowest value occurred. For this program, instead of setting the initial minimum value to the first value in the file, use the maximum value for a double. The Java Class Library provides this value as constant in the Double wrapper class. Be sure to handle the case of the file being empty. can someone help me answer this?
Explanation / Answer
/**
* The java program Lowest that reads a input text file
* dja.txt(assumeed it has 7 double type values)
* and read into an array . Set lowest as the MAX_VALUE
* from wrapper class. Then print the lowest average value
* and day on which the lowest average is set.
* */
//Lowest.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Lowest {
public static void main(String[] args)
{
//set DAYS=7
final int DAYS=7;
//declare an array of type Double
Double[] avg=new Double[DAYS];
//set lowest as MAX_VALUE of Double Wrapper class
Double lowest=Double.MAX_VALUE;
//set lowesetDay=0;
int lowesetDay=0;
//declare a variable Scanner of class
Scanner filescanner=null;
//try-catch block that checks if file "dja.txt"
//exists or not .If not exists then thows a exception
try
{
//create an instance of Scanner class to read file
filescanner=new Scanner(new File("dja.txt"));
int day=0;
//read values from file and update lowest and lowestDay
while(day<DAYS)
{
//read and convert value from string to double
avg[day]=Double.parseDouble(filescanner.nextLine());
if(avg[day]<lowest)
{
lowest=avg[day];
lowesetDay=day;
}
//increment the day by one
day++;
}
//print Lowest average
System.out.println("Lowest average : "+lowest);
//print lowestDay value
System.out.println("Day: "+(lowesetDay+1));
}
//Catch the file not found exception
catch (FileNotFoundException e)
{
System.out.println(e);
}
}//end of main method
}//end of the classLowest
---------------------------------
Text file : dja.txt
25.653
58.596
11.48
2693.5
15.95
1583.2
45.36
---------------------------------
Output:
Lowest average : 11.48
Day: 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.