import java.io.File; import java.io.FileNotFoundException; import java.util.Scan
ID: 3534253 • Letter: I
Question
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
?
?
public class Numbers {
?
?
public static void main(String[] args) {
?
?
try {
File file = new File("C: umbers.txt");
?
?
Scanner input = new Scanner(file);
int count = 0;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
while (input.hasNext()) {
int next = input.nextInt();
min = Math.min(min, next);
// Update maximum
max = Math.max(max, next);
}
input.close();
System.out.println("Smallest number "+min);
System.out.println("Largest number "+max);
?
?
} catch (FileNotFoundException ex) {
System.out.println("Could not find the output file");
}
}
}
I then have a file numbers.txt, whch has numbers of all sorts in it. So it does NOT find my file,
Explanation / Answer
by the way, your code is correct. Just check you have placed your file in C drive named as numbers.txt
And the file contains only number
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.