2017-04-15 3 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

그래서 try/catch 블록과 함께 input.readLine의 첫 번째 인스턴스를 제거하면 NullPointerException이 발생합니다. 나는 그것이 왜 선과 함께하고 왜 다음에하지 않는지 이해하지 못한다고 생각합니다. –

+0

input.readLine()의 첫 번째 인스턴스를 제거하면 더 이상 파일 끝을 확인하지 않습니다. 따라서 input.readLine()이 파일의 끝에 도달하면 종료하는 대신 null을 반환합니다. 이것이 NullPointerException을 얻는 이유입니다. –