0
我已經在互聯網上搜索了大約一個半小時,而我無法爲我的生活找出我出錯的地方。導入.dat文件Java
我的問題是,每次我嘗試並運行它時,我都會收到一個錯誤,直到它搜索到文件而且沒有失敗,它會回覆「找不到文件」。我在一個MAC我認爲我正確地輸入目錄,但東西是搞砸了。
(當打開numEven.dat) 對於我的輸入,我試過「numEven.dat」(放置dat文件在與java文件相同的目錄中)
我也試過「/Users/java/numEven.dat」和「Users/java/numEven.dat」 我知道它在那個目錄下。我究竟做錯了什麼?
主類文件:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class StatDriver
{
public static void main(String[] args)
{
String fileName = "";
Scanner scan = new Scanner(System.in);
double[] array = new double[20];
System.out.print(" Enter file name: ");
fileName = scan.next();
System.out.println("\n \n \n \n My Grades - View Statistics");
System.out.println(" ------------------------");
// int valueCount = readFile(array,fileName);
array = readFile(array, fileName);
Stat stat = new Stat(array, array.length);
// call each calc on Stat class and display results for each method
stat.calcAvg();
stat.calcMedian();
stat.findMax();
stat.findMin();
// print the return values for each of the above out to the user
}
public static double[] readFile(double[] array, String fileName)
{
int valueCount = 0;
FileIO importFile = new FileIO();
importFile.main(array, fileName);
System.out.println(array);
valueCount = array.length;
// return valueCount;
return array;
}
}
FileIO專注等級:
import java.util.Scanner;
import java.io.*;
public class FileIO
{
public void main (double[] array, String fileName)
{
double [] num = new double[5];
Scanner inFile;
int i = 0;
try
{
System.out.println(fileName);
inFile = new Scanner(new File("fileName"));
while(inFile.hasNextDouble())
{
array[i] = inFile.nextDouble();
i++;
}
inFile.close();
for(int x = 0; x < i; x++)
System.out.println(" " + num[x]);
}
catch(FileNotFoundException e)
{
System.out.println (" File not found");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println (" array index too large");
}
}
}
此外,'FileIO.main'方法應該有一個不同的名稱。 –
是的,完全忘了談論那個 – Alex
是的,完全忘了談論那個 – Alex