Write a Java program that inputs 7 double values from a file dja.txt that repres
ID: 3761205 • 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.
Explanation / Answer
/**The java program that reads a text file dja.txt and finds the minimum value and day on which
* value the minimum is found. Print the minimum and day value*/
//JoeDonesProgram.java
import java.io.File;
import java.util.Scanner;
public class JoeDonesProgram
{
public static void main(String[] args)
{
//set double MAX_VALUE to minimum
double minimum=Double.MAX_VALUE;
//set index to negative
double dayIndex=-1;
//set scanner to null
Scanner file=null;
//set count to 1
int count=1;
try
{
//open a file named as "dja.txt"
file=new Scanner(new File("dja.txt"));
//Read input double value until is double vlaue
while(file.hasNextDouble())
{
//read double value
double value=file.nextDouble();
//check if value is less than minimum
if(value<minimum)
{
//set minimum value
minimum=value;
//set count to dayIndex
//on which minimum is found
dayIndex=count;
}
//increment the count by one.
count++;
}
}
catch (Exception e)
{
System.out.println(e);
}
//print minimum value
System.out.println("Minimum value :"+minimum);
//set dayIndex value
System.out.println("Day on which minimum value : "+dayIndex);
}
}//end of the class
-----------------------------------------------------------------------------------------------
Sample input file dja.txt
45 67 54 98 65 85 35
Sample output:
Minimum value :35.0
Day on which minimum value : 7.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.