为什么我的Buffered Reader跳过线

时间:2017-04-15 03:13:27

标签: java bufferedreader

我正在为我的编程类开发一个项目,我遇到了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 个答案:

答案 0 :(得分:0)

你的猜测绝对正确。

这会跳过,因为while循环中的第一个条件是读取一行

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

但是您没有对读入行做任何事情。

然后下次你打电话

input.readLine();

您阅读了下一行,但实际上您对该行做了一些事情。

如需进一步参考,请尝试查看此主题:

Java: How to read a text file