2017-04-15 17 views
0

プログラミングクラスのプロジェクトで作業していますが、BufferedReaderに問題があります。ここにコードがあります。それは正常に実行されますが、私のCSVファイル内の他のすべての行を読むだけです。私は問題があると思うinFile = input.readLineそこに2回が、私はそれらの1つを削除すると、私はランタイムエラーが発生します。バッファドリーダーが行をスキップする理由

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

なぜちょうど1だけキャッチ..試すに入れませんか? – abcOfJavaAndCPP

答えて

0

あなたの推測は間違いありません。

あなたのwhileループの最初の条件は、行を読み込み

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

いますが、リードインラインで何もしないので、これはスキップします。

は、その後、あなたが

input.readLine(); 

を呼び出す次回は、あなたは、次の行を読んで、しかし、あなたは実際にラインで何かを行います。さらに参考のために

、このスレッドを見てみてください:

Java: How to read a text file

+0

したがって、try/catchブロックとともにinput.readLineの最初のインスタンスを削除すると、NullPointerExceptionが発生します。私は、なぜそれが行と何かをし、次回はそれをしない理由を理解していないと思います。 –

+0

input.readLine()の最初のインスタンスを削除すると、もはやファイルの終わりをチェックしなくなります。したがって、input.readLine()がファイルの終わりに達すると、終了する代わりにnullが返されます。これがNullPointerExceptionを取得している理由です –

関連する問題