This is my code:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class ArrayAnalyser {
public static void main(String[] args) throws IOException {
int size;
double[] array;
String fileName = "C:\\Users\\kengock\\Desktop\\Numbers.txt";
size = getLineNumber(fileName);
array = new double[size];
File file = new File(fileName);
Scanner infile = new Scanner(file);
int index = 0;
while (infile.hasNext()) {
array[index] = infile.nextDouble();
index++;
}
infile.close();
System.out.println("The min is " + getSmallest(array));
System.out.println("The average is " + getAverage(array));
}
public static int getLineNumber(String fileName) throws IOException {
int size = 0;
File file = new File(fileName);
Scanner infile = new Scanner(file);
while (infile.hasNext()) {
size++;
infile.nextLine();
}
infile.close();
return size;
}
public static double getSmallest(double[] array) {
double min = array[0];
for (int i = 0; i < array.length; i++) {
if (array[i] < min)
min = array[i];
}
return min;
}
public static double getAverage(double[] array) {
double sum = 0;
for (int i = 0; i < array.length; i++) {
sum += (array[i]);
}
return sum / array.length;
}
}
This is the error message I'm getting:
C:\\Users\\kengock\\Desktop>java ArrayAnalyser Exception in thread "main" java.io.FileNotFoundException: C:\\Users\\kengock\\Desktop (存取被拒。) at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(FileInputStream.java:212) at java.base/java.io.FileInputStream.(FileInputStream.java:154) at java.base/java.util.Scanner.(Scanner.java:639) at ArrayAnalyser.getLineNumber(ArrayAnalyser.java:33) at ArrayAnalyser.main(ArrayAnalyser.java:13)
What should I do with the file path?
In the below line of code you have only mentioned the path of the file but not the name like abc.txt
String fileName="C:\\Users\\kengock\\Desktop";
It should be like :-
String fileName="C:\\Users\\kengock\\Desktop\\abc.txt";
public class ArrayAnalyser {
public static void main(String[] args) throws IOException {
File file = new File("c:/Users/kengock/Desktop/Numbers.txt");
double[] arr = readArrayFromFile(file);
System.out.println(Arrays.toString(arr));
System.out.format(Locale.ENGLISH, "The min is %.2f\n", getSmallest(arr));
System.out.format(Locale.ENGLISH, "The average is %.2f\n", getAverage(arr));
}
public static double[] readArrayFromFile(File file) throws FileNotFoundException {
List<Double> numbers = new ArrayList<>();
try (Scanner scan = new Scanner(file)) {
scan.useLocale(Locale.ENGLISH);
while (scan.hasNext())
numbers.add(scan.nextDouble());
}
return numbers.stream().mapToDouble(i -> i).toArray();
}
public static double getSmallest(double[] arr) {
return Arrays.stream(arr).min().orElse(0);
}
public static double getAverage(double[] arr) {
return Arrays.stream(arr).average().orElse(0);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.