fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args) throws FileNotFoundException {
  11. File cityPopulation = new File("cityPopulationData.txt");
  12. Scanner fileReader = new Scanner(cityPopulation);
  13. fileReader.useDelimiter("[\t|\n]+");
  14. String[] cities = new String[400];
  15. int[] pop2010 = new int[400];
  16. int[] pop2013 = new int[400];
  17. double[] area = new double[400];
  18. int count = getData(fileReader, cities, pop2010, pop2013, area);
  19. displayArrays(cities, pop2010, pop2013, area, count);
  20. largestCity(pop2010, count);
  21. }
  22.  
  23. public static int getData(Scanner inf, String[] c, int[] pop10, int[] pop13, double[] a) {
  24. int count = 0;
  25. inf.next();
  26. inf.next();
  27. inf.next();
  28. inf.next();
  29. while(inf.hasNext()) {
  30. c[count] = inf.next();
  31. pop10[count] = inf.nextInt();
  32. pop13[count] = inf.nextInt();
  33. a[count] = inf.nextDouble();
  34. count++;
  35. }
  36. return count;
  37. }
  38.  
  39. public static void displayArrays(String[] c, int[] pop10, int[] pop13, double[] a, int count) {
  40. for(int i = 0; i < count; i++){
  41. System.out.printf("%s \t %d \t %d \t %f", c[i], pop10[i], pop13[i], a[i]);
  42. }
  43. }
  44.  
  45. public static int largestCity(int[] pop10, int count) {
  46. int lCindex = 0;
  47. for(int i = 1; i < count; i++) {
  48. if(pop10[i] > pop10[lCindex])
  49. lCindex = i;
  50. }
  51. return lCindex;
  52. }
  53.  
  54. // public static int findGrowth(int[] pop10, int[] pop13, int count, ) {
  55. //
  56. // }
  57.  
  58. public static int highestDensity(int[] pop10, double[] area, int count) {
  59. int hDindex = 0;
  60. for( int i = 1; i < count; i++) {
  61. if ((pop10[i]/area[i]) > (pop10[hDindex]/area[hDindex]))
  62. hDindex = i;
  63. }
  64. return hDindex;
  65. }
  66.  
  67. }
Runtime error #stdin #stdout #stderr 0.07s 380160KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.io.FileNotFoundException: cityPopulationData.txt (No such file or directory)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(FileInputStream.java:138)
	at java.util.Scanner.<init>(Scanner.java:656)
	at Ideone.main(Main.java:12)