2017-04-15 80 views
0

我正在为我的编程类工作,并且遇到了BufferedReader问题。这是代码。它会运行良好,但它只是读取我的csv文件中的每隔一行。我认为问题是我有inFile = input.readLine那里两次,但如果我删除其中一个我得到运行时错误。为什么我的缓冲阅读器跳过行

import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.*; 
import java.io.BufferedReader; 




public class CrimeData { 
    public static void main(String[] args) throws FileNotFoundException { 
    BufferedReader input = new BufferedReader(new FileReader("crime.csv")); 
    String inFile; 
try { 
    inFile = input.readLine(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

    int n = 0; 
    int year[] = new int[50], population[] = new int[50], violentCrime[] = new int[50]; 
    double violentCrimeRate[] = new double[50]; 
    int murderAndNonnegligentManslaughter[] = new int[50]; 
    double murderAndNonnegligentManslaughterRate[] = new double[50]; 
    int rape[] = new int[50]; 
    double rapeRate[] = new double[50]; 
    int robbery[] = new int[50]; 
    double robberyRate[] = new double[50]; 
    int aggravatedAssault[] = new int[50]; 
    double aggravatedAssaultRate[] = new double[50]; 
    int propertyCrime[] = new int[50]; 
    double propertyCrimeRate[] = new double[50]; 
    int burglary[] = new int[50]; 
    double burglaryRate[] = new double[50]; 
    int larcenyTheft[] = new int[50]; 
    double larcenyTheftRate[] = new double[50]; 
    int motorVehicleTheft[] = new int[50]; 
    double motorVehicleTheftRate[] = new double[50]; 

    try { 
    while ((inFile = input.readLine()) != null) { 

      String words[] = input.readLine().split(","); 
      year[n] = Integer.parseInt(words[0]); 
      population[n] = Integer.parseInt(words[1]); 
      violentCrime[n] = Integer.parseInt(words[2]); 
      violentCrimeRate[n] = Double.parseDouble(words[3]); 
      murderAndNonnegligentManslaughter[n] = Integer.parseInt(words[4]); 
      murderAndNonnegligentManslaughterRate[n] = Double.parseDouble(words[5]); 
      rape[n] = Integer.parseInt(words[6]); 
      rapeRate[n] = Double.parseDouble(words[7]); 
      robbery[n] = Integer.parseInt(words[8]); 
      robberyRate[n] = Double.parseDouble(words[9]); 
      aggravatedAssault[n] = Integer.parseInt(words[10]); 
      aggravatedAssaultRate[n] = Double.parseDouble(words[11]); 
      propertyCrime[n] = Integer.parseInt(words[12]); 
      propertyCrimeRate[n] = Double.parseDouble(words[13]); 
      burglary[n] = Integer.parseInt(words[14]); 
      burglaryRate[n] = Double.parseDouble(words[15]); 
      larcenyTheft[n] = Integer.parseInt(words[16]); 
      larcenyTheftRate[n] = Double.parseDouble(words[17]); 
      motorVehicleTheft[n] = Integer.parseInt(words[18]); 
      motorVehicleTheftRate[n] = Double.parseDouble(words[19]); 
      n++; 
     } 
} catch (NumberFormatException | IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 


    Scanner scan = new Scanner(System.in); 
    while (true) { 
     System.out.println("********** Welcome to the US Crime Statistical Application **************************"); 
     System.out.println("Enter the number of the question you want answered. "); 
     System.out.println("1. What were the percentages in population growth for each consecutive year from 1994 – 2013?"); 
     System.out.println("2. What year was the Murder rate the highest?"); 
     System.out.println("3. What year was the Murder rate the lowest?"); 
     System.out.println("4. What year was the Robbery rate the highest?"); 
     System.out.println("5. What year was the Robbery rate the lowest?"); 
     System.out.println("6. What was the total percentage change in Motor Vehicle Theft between 1998 and 2012?"); 
     System.out.println("7. What was [enter your first unique statistic here]?"); 
     System.out.println("8. What was [enter your second unique statistic here]?"); 
     System.out.println("9. Quit the program"); 
     System.out.println("Enter your selection: "); 
     int choice = scan.nextInt(); 
     double low, high, percent; 
     int y; 
     switch (choice) { 
     case 1: 
      for (int i = 1; i < n; i++) { 
       percent = ((population[i] - population[i - 1])/population[i - 1]) * 100; 
       System.out.println(
         "Percentage of population growth during " + year[i - 1] + "-" + year[i] + " :" + percent); 
      } 
      break; 
     case 2: 
      high = murderAndNonnegligentManslaughter[0]; 
      y = year[0]; 
      for (int i = 1; i < n; i++) { 
       if (murderAndNonnegligentManslaughter[i] > high) { 
        high = murderAndNonnegligentManslaughter[i]; 
        y = year[i]; 
       } 
      } 
      System.out.println("The year has the highest Murder rate : " + y); 
      break; 
     case 3: 
      low = murderAndNonnegligentManslaughter[0]; 
      y = year[0]; 
      for (int i = 1; i < n; i++) { 
       if (murderAndNonnegligentManslaughter[i] < low) { 
        low = murderAndNonnegligentManslaughter[i]; 
        y = year[i]; 
       } 
      } 
      System.out.println("The year has the lowest Murder rate : " + y); 
      break; 
     case 4: 
      high = robberyRate[0]; 
      y = year[0]; 
      for (int i = 1; i < n; i++) { 
       if (robberyRate[i] > high) { 
        high = robberyRate[i]; 
        y = year[i]; 
       } 
      } 
      System.out.println("The year has the highest Robbery rate : " + y); 
      break; 
     case 5: 
      low = robberyRate[0]; 
      y = year[0]; 
      for (int i = 1; i < n; i++) { 
       if (robberyRate[i] < low) { 
        low = robberyRate[i]; 
        y = year[i]; 
       } 
      } 
      System.out.println("The year has the lowest Robbery rate : " + y); 
      break; 
     case 6: 
      double rateChange = 0; 
      rateChange = (motorVehicleTheft[19] - motorVehicleTheft[5]); 
      System.out.println(motorVehicleTheft); 


     case 7: 
      break; 
     case 8: 
      break; 
     case 9: 
      System.out.println("Thank you for using the Crime Database"); 
      System.exit(0); 

     } 
    } 
} 
} 
+1

为什么不干脆把它放在一个一个尝试..赶上而已? – abcOfJavaAndCPP

回答

0

你的猜测是绝对正确的。

这将跳过,因为在你的while循环的首要条件读取一行,

while((infile = input.readLine()) != null){ 

但你没有做的读入行任何东西。

那么下次你打电话

input.readLine(); 

你读下一行,但实际上你做的东西线。

更多参考,尝试寻找这些线:

Java: How to read a text file

+0

因此,当我删除input.readLine的第一个实例以及try/catch块时,我得到一个NullPointerException。我想我只是不明白为什么它在线上做了些什么,下一次没有做。 –

+0

当您删除input.readLine()的第一个实例时,您不再检查文件的结尾。因此,当input.readLine()命中文件末尾时,它将返回null而不是终止。这就是为什么你得到NullPointerException –