eclipse - File not found exception - Java -


this question has answer here:

i writing code parallel arrays , when read in data , try display error comes up. ga teaches lab not see problem code , said should try here. here code , error right after it. 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 = 0; < 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 = 1; < 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 = 1; < 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 file specified)     @ java.io.fileinputstream.open(native method)     @ java.io.fileinputstream.<init>(unknown source)     @ java.util.scanner.<init>(unknown source)     @ parallelarrays.main(parallelarrays.java:7) 

well exception self explanatory "java.io.filenotfoundexception"
==>file citypopulation = new file("citypopulationdata.txt");

since using eclipse, try giving absolute path of file c:/data/filex.txt etc. work. after create resource folder in project tree , put file there. use classloader.getresouceasstream load file. following method used when want relative access file..you can more info online regaarding method.

hope helps


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -