Get doubles from input file. Output the biggest import java.io.File; import java
ID: 3881893 • Letter: G
Question
Get doubles from input file. Output the biggest
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Lab8Num1 {
public static void main(String[] args) {
File inFile = new File("lab8.in");
Scanner fileInput = null;
try {
fileInput = new Scanner(inFile);
} catch (FileNotFoundException ex) {
//Logger.getLogger(Lab10.class.getName()).log(Level.SEVERE, null, ex);
}
//get first number and make it the biggest;
//loop through the file
//getting doubles and comparing with current "biggest"
System.out.println("The biggest number was " + biggest);
}
}
Explanation / Answer
The program is as follows:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Lab8Num1 {
public static void main(String[] args) {
//Declaring variable to be used for storing and for output
double biggest,temp;
//Creating file to read numbers
File inFile = new File("lab8.txt");
//Stream to read data from file
Scanner fileInput = null;
try {
fileInput = new Scanner(inFile);
} catch (FileNotFoundException ex) {
//Logger.getLogger(Lab10.class.getName()).log(Level.SEVERE, null, ex);
}
//get first number and make it the biggest;
biggest=fileInput.nextDouble();
//loop through the file
while(fileInput.hasNextLine())
{
//getting doubles and comparing with current "biggest"
temp=fileInput.nextDouble();
if(temp>biggest)
biggest=temp;
}
System.out.println("The biggest number was " + biggest);
}
}
Output
The biggest number was 10.6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.