-1

I am writing a code with parallel arrays and when I read in the data and try to display it this error comes up. the GA that teaches my lab does not see a problem with my code and said I should try here. here is my code and the error will be right after it. I am using eclipse.

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:

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)
simo.3792
  • 2,102
  • 1
  • 17
  • 29

1 Answers1

0

Well the exception is self explanatory "java.io.FileNotFoundException"
==>File cityPopulation = new File("cityPopulationData.txt");

since you are using eclipse, try giving a absolute path of file like c:/data/filex.txt etc. It will work. After that create a resource folder in your project tree and put the file there. Use ClassLoader.getresouceAsStream to load the file. the following method is used when you want relative access to your file..you can get more info online regaarding this method.

hope this helps

Manish
  • 99
  • 8