2014-12-02 78 views
-1

我寫並行陣列代碼,當我讀入數據,並嘗試以顯示它這個錯誤出現。教我實驗室的GA沒有看到我的代碼有問題,並說我應該在這裏嘗試。這裏是我的代碼,錯誤會在它後面。我正在使用eclipse。文件未發現異常 - Java的

import java.util.Scanner; 
import java.io.File; 
import java.io.FileNotFoundException; 
public class parallelArrays { 
    public static void main(String[] args) throws FileNotFoundException { 
     File cityPopulation = new File("cityPopulationData.txt"); 
     Scanner fileReader = new Scanner(cityPopulation); 
     fileReader.useDelimiter("[\t|\n]+"); 
     String[] cities = new String[400]; 
     int[] pop2010 = new int[400]; 
     int[] pop2013 = new int[400]; 
     double[] area = new double[400]; 
     int count = getData(fileReader, cities, pop2010, pop2013, area); 
     displayArrays(cities, pop2010, pop2013, area, count); 
     largestCity(pop2010, count); 
    } 

    public static int getData(Scanner inf, String[] c, int[] pop10, int[] pop13, double[] a) { 
     int count = 0; 
     inf.next(); 
     inf.next(); 
     inf.next(); 
     inf.next(); 
     while(inf.hasNext()) { 
      c[count] = inf.next(); 
      pop10[count] = inf.nextInt(); 
      pop13[count] = inf.nextInt(); 
      a[count] = inf.nextDouble(); 
      count++; 
     } 
     return count; 
    } 

    public static void displayArrays(String[] c, int[] pop10, int[] pop13, double[] a, int count) { 
     for(int i = 0; i < count; i++){ 
      System.out.printf("%s \t %d \t %d \t %f", c[i], pop10[i], pop13[i], a[i]); 
     } 
    } 

    public static int largestCity(int[] pop10, int count) { 
     int lCindex = 0; 
     for(int i = 1; i < count; i++) { 
      if(pop10[i] > pop10[lCindex]) 
       lCindex = i; 
     } 
     return lCindex; 
    } 

    // public static int findGrowth(int[] pop10, int[] pop13, int count, ) { 
    // 
    // } 

    public static int highestDensity(int[] pop10, double[] area, int count) { 
     int hDindex = 0; 
     for(int i = 1; i < count; i++) { 
      if ((pop10[i]/area[i]) > (pop10[hDindex]/area[hDindex])) 
       hDindex = i; 
     } 
     return hDindex; 
    } 
} 

例外:

Exception in thread "main" java.io.FileNotFoundException: cityPopulationData.txt (The system cannot find the file specified) 
    at java.io.FileInputStream.open(Native Method) 
    at java.io.FileInputStream.<init>(Unknown Source) 
    at java.util.Scanner.<init>(Unknown Source) 
    at parallelArrays.main(parallelArrays.java:7) 
+2

嘗試使用絕對路徑 – Nikolai97 2014-12-02 00:05:12

+4

或保存項目的文件夾 – enrique7mc 2014-12-02 00:05:59

+0

教師中的文件發送你左右。經典。 – csmckelvey 2014-12-02 00:08:27

回答

0

井例外是自我解釋 「java.io.FileNotFoundException」
==>文件cityPopulation =新的文件( 「cityPopulationData.txt」);

,因爲使用的是Eclipse,試着給喜歡C文件的絕對路徑:/data/filex.txt等,這會工作。之後,在項目樹中創建一個資源文件夾,並將文件放在那裏。使用ClassLoader.getresouceAsStream來加載文件。當你想要相對訪問你的文件時使用下面的方法..你可以在線獲得更多的信息regaarding這種方法。

希望這有助於