0

I am working on a project for my programming class and I am having trouble with the BufferedReader. Here is the code. It will run fine but it is only reading every other line in my csv file. I think the issue is I have inFile = input.readLine in there twice but if I remove one of them the I get runtime errors.

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 Answers1

0

Your guess is absolutely correct.

This skips because the first condition in your while loop reads a line,

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

but you do not do anything with the read-in line.

Then the next time you call

input.readLine();

You read the next line, but you actually do something with the line.

For further reference, try looking at this thread:

Java: How to read a text file

Community
  • 1
  • 1
Nick Meyer
  • 181
  • 6
  • So when I remove the first instance of input.readLine along with the try/catch block I get a NullPointerException. I guess I just don't understand why it does something with the line and the next time it doesn't. – A Michael Anderson Apr 15 '17 at 03:43
  • When you remove that first instance of input.readLine(), you are no longer checking for the end of file. Therefore, when input.readLine() hits the end of file it will return null, instead of terminating. This is why you are getting NullPointerException – Nick Meyer Apr 15 '17 at 03:48