Вот рабочая версия.Я переписал разделы из вашего кода, которые были проблемными / нуждались в исправлении.Обратите внимание, что я использовал потоковую передачу Java 8 и лямбду для краткости кода.
Crimes.csv должен выглядеть примерно так, как показано ниже, но из вашего кода, я думаю, у вас есть годовые данные за 1994 год- период 2013 года
year,population,violentCrime,violentCrimeRate,murder,murderRate,rape,rapeRate,robbery,robberyRate,aggravatedAssault,aggravatedAssaultRate,propertyCrime,propertyCrimeRate,burglary,burglaryRate,larcenyTheft,larcenyTheftRate,motorVehicleTheft,motorVehicleTheftRate
2001,2000,20,0.01,30,0.001,25,0.012,20,0.01,20,0.01,20,0.01,20,0.01,20,0.01,20,0.01
2002,20100,20,0.01,30,0.001,25,0.012,20,0.01,20,0.01,20,0.01,20,0.01,20,0.01,20,0.01
2003,20500,20,0.01,30,0.001,25,0.012,20,0.01,20,0.01,20,0.01,20,0.01,20,0.01,20,0.01
2004,21050,20,0.01,30,0.001,25,0.012,20,0.01,20,0.01,20,0.01,20,0.01,20,0.01,20,0.01
CrimeDataClass.java
package crimedata;
public class CrimeDataClass {
// Variable for the class
private int year = 1994;
private int population = 0;
private int violentCrime = 0;
private double violentCrimeRate = 0.0;
private int murder = 0;
private double murderRate = 0.0;
private int rape = 0;
private double rapeRate = 0.0;
private int robbery = 0;
private double robberyRate = 0.0;
private int aggravatedAssault = 0;
private double aggravatedAssaultRate = 0.0;
private int propertyCrime = 0;
private double propertyCrimeRate = 0.0;
private int burglary = 0;
private double burglaryRate = 0.0;
private int larcenyTheft = 0;
private double larcenyTheftRate = 0.0;
private int motorVehicleTheft = 0;
private double motorVehicleTheftRate = 0.0;
// Default constructor
public CrimeDataClass() {
} // End Constructor
// Full Parameterize Constructor
public CrimeDataClass(int year, int population, int violentCrime, double violentCrimeRate, int murder,
double murderRate, int rape, double rapeRate, int robbery, double robberyRate, int aggravatedAssault,
double aggravatedAssaultRate, int propertyCrime, double propertyCrimeRate, int burglary,
double burglaryRate, int larcenyTheft, double larcenyTheftRate, int motorVehicleTheft,
double motorVehicleTheftRate) {
this.year = year;
this.population = population;
this.violentCrime = violentCrime;
this.violentCrimeRate = violentCrimeRate;
this.murder = murder;
this.murderRate = murderRate;
this.rape = rape;
this.rapeRate = rapeRate;
this.robbery = robbery;
this.robberyRate = robberyRate;
this.aggravatedAssault = aggravatedAssault;
this.aggravatedAssaultRate = aggravatedAssaultRate;
this.propertyCrime = propertyCrime;
this.propertyCrimeRate = propertyCrimeRate;
this.burglary = burglary;
this.burglaryRate = burglaryRate;
this.larcenyTheft = larcenyTheft;
this.larcenyTheftRate = larcenyTheftRate;
this.motorVehicleTheft = motorVehicleTheft;
this.motorVehicleTheftRate = motorVehicleTheftRate;
} // End Constructor
// Getter Method
public int getYear() {
return this.year;
}
public int getPopulation() {
return this.population;
}
public int getViolentCrime() {
return this.violentCrime;
}
public double getViolentCrimeRate() {
return this.violentCrimeRate;
}
public int getMurder() {
return this.murder;
}
public double getMurderRate() {
return this.murderRate;
}
public int getRape() {
return this.rape;
}
public double getRapeRate() {
return this.rapeRate;
}
public int getRobbery() {
return this.robbery;
}
public double getRobberyRate() {
return this.robberyRate;
}
public int getAggravatedAssault() {
return this.aggravatedAssault;
}
public double getAggravatedAssaultRate() {
return this.aggravatedAssaultRate;
}
public int getPropertyCrime() {
return this.propertyCrime;
}
public double getPropertyCrimeRate() {
return this.propertyCrimeRate;
}
public int getBurglary() {
return this.burglary;
}
public double getBurglaryRate() {
return this.burglaryRate;
}
public int getLarcenyTheft() {
return this.larcenyTheft;
}
public double getLarcenyTheftRate() {
return this.larcenyTheftRate;
}
public int getMotorVehicleTheft() {
return this.motorVehicleTheft;
}
public double getMotorVehicleTheftRate() {
return this.motorVehicleTheftRate;
} // End Getter Method
// Setter Method
// Objects are unchangeable due to using values from CSV
// toString Method
public String toString() {
return String.format(
"\nYear: %d" + "\nPopulation: %d" + "\nViolent Crime: %d" + "\nViolent Crime Rate: %.2f"
+ "\nMurder: %d" + "\nMurder Rate: %.2f" + "\nRape: %d" + "\nRape Rate: %.2f" + "\nRobbery: %d"
+ "\nRobbery Rate: %.2f" + "\nAggravated Assault: %d" + "\nAggravated Assault Rate: %.2f"
+ "\nProperty Crime: %d" + "\nProperty Crime Rate: %.2f" + "\nBurglary: %d"
+ "\nBurglary Rate: %.2f" + "\nLarceny-Theft: %d" + "\nLarceny-Theft Rate: %.2f"
+ "\nMotor Vehicle Theft: %d" + "\nMotor Vehicle Theft Rate: %.2f",
this.year, this.population, this.violentCrime, this.violentCrimeRate, this.murder, this.murderRate,
this.rape, this.rapeRate, this.robbery, this.robberyRate, this.aggravatedAssault,
this.aggravatedAssaultRate, this.propertyCrime, this.propertyCrimeRate, this.burglary,
this.burglaryRate, this.larcenyTheft, this.larcenyTheftRate, this.motorVehicleTheft,
this.motorVehicleTheftRate);
} // End toString Method
} // End Class
CrimeDataArray.java
package crimedata;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;
public class CrimeDataArray {
private CrimeDataClass[] crimes;
private boolean isFileRead;
private String fileLocation;
public CrimeDataArray() {
this.fileLocation = "<add-the-full-path-to-Crimes.csv>";
}
public CrimeDataArray(String fileLocation) {
this.fileLocation = fileLocation;
}
public void readFile() {
try {
this.crimes =
Files.readAllLines(Paths.get(fileLocation))
.stream().skip(1).map(line -> line.split(","))
.map(tokens -> new CrimeDataClass(
Integer.parseInt(tokens[0]),
Integer.parseInt(tokens[1]),
Integer.parseInt(tokens[2]),
Double.parseDouble(tokens[3]),
Integer.parseInt(tokens[4]),
Double.parseDouble(tokens[5]),
Integer.parseInt(tokens[6]),
Double.parseDouble(tokens[7]),
Integer.parseInt(tokens[8]),
Double.parseDouble(tokens[9]),
Integer.parseInt(tokens[10]),
Double.parseDouble(tokens[11]),
Integer.parseInt(tokens[12]),
Double.parseDouble(tokens[13]),
Integer.parseInt(tokens[14]),
Double.parseDouble(tokens[15]),
Integer.parseInt(tokens[16]),
Double.parseDouble(tokens[17]),
Integer.parseInt(tokens[18]),
Double.parseDouble(tokens[19])
))
.collect(Collectors.toList()).toArray(new CrimeDataClass[0])
;
isFileRead = true;
} catch (IOException e) {
throw new RuntimeException("CSV file read failed, error: "+e.getMessage(), e);
}
}
public int countRows(String dataFile) {
if(!isFileRead) {
throw new RuntimeException("Data not loaded yet, call readFile() first");
}// you can use this in all methods if you wish a safety net against NullPointerException
return crimes.length;
}
// getPopulationGrowth
public void getPopulationGrowth() {
/* Gro: not changed */
for (int i = 0; i < (crimes.length - 2); i++) {
System.out.printf("\n%d - %d: %.4f%%", crimes[i].getYear(), crimes[i + 1].getYear(),
((crimes[i + 1].getPopulation() - crimes[i].getPopulation()) / (float) crimes[i].getPopulation())
* 100);
} // End For
} // End getPopulationGrowth
// getMaxViolentCrimeRate
public int getMaxViolentCrimeRate() {
return Arrays.stream(crimes)
.max(Comparator.comparing( CrimeDataClass::getViolentCrimeRate))
.get().getYear();
} // End getMaxViolentCrimeRate
// getMinViolentCrimeRate
public int getMinViolentCrimeRate() {
return Arrays.stream(crimes)
.min(Comparator.comparing( CrimeDataClass::getViolentCrimeRate))
.get().getYear();
} // End getMinViolentCrimeRate
// getMaxMurderRate
public int getMaxMurderRate() {
return Arrays.stream(crimes)
.max(Comparator.comparing( CrimeDataClass::getMurderRate))
.get().getYear();
} // End getMaxMurderRate
// getMinMurderRate
public int getMinMurderRate() {
return Arrays.stream(crimes)
.min(Comparator.comparing( CrimeDataClass::getMurderRate))
.get().getYear();
} // End getMinMurderRate
// getMaxRapeRate
public int getMaxRapeRate() {
return Arrays.stream(crimes)
.max(Comparator.comparing( CrimeDataClass::getRapeRate))
.get().getYear();
} // End getMaxRapeRate
// getMinRapeRate
public int getMinRapeRate() {
return Arrays.stream(crimes)
.min(Comparator.comparing( CrimeDataClass::getRapeRate))
.get().getYear();
} // End getMinRapeRate
// getMaxRobberyRate
public int getMaxRobberyRate() {
return Arrays.stream(crimes)
.max(Comparator.comparing( CrimeDataClass::getRobberyRate))
.get().getYear();
} // End getMaxRobberyRate
// getMinRobberyRate
public int getMinRobberyRate() {
return Arrays.stream(crimes)
.min(Comparator.comparing( CrimeDataClass::getRobberyRate))
.get().getYear();
} // End getMinRobberyRate
// getMaxAggravatedAssaultRate
public int getMaxAggravatedAssaultRate() {
return Arrays.stream(crimes)
.max(Comparator.comparing( CrimeDataClass::getAggravatedAssault))
.get().getYear();
} // End getMaxAggravatedAssaultRate
// getMinAggravatedAssaultRate
public int getMinAggravatedAssaultRate() {
return Arrays.stream(crimes)
.min(Comparator.comparing( CrimeDataClass::getAggravatedAssault))
.get().getYear();
} // End getMinAggravatedAssaultRate
// getMaxPropertyCrimeRate
public int getMaxPropertyCrimeRate() {
return Arrays.stream(crimes)
.max(Comparator.comparing( CrimeDataClass::getPropertyCrimeRate))
.get().getYear();
} // End getMaxPropertyCrimeRate
// getMinPropertyCrimeRate
public int getMinPropertyCrimeRate() {
return Arrays.stream(crimes)
.min(Comparator.comparing( CrimeDataClass::getPropertyCrimeRate))
.get().getYear();
} // End getMinPropertyCrimeRate
// getMaxBurglaryRate
public int getMaxBurglaryRate() {
return Arrays.stream(crimes)
.max(Comparator.comparing( CrimeDataClass::getBurglaryRate))
.get().getYear();
} // End getMaxBurglaryRate
// getMinBurglaryRate
public int getMinBurglaryRate() {
return Arrays.stream(crimes)
.min(Comparator.comparing( CrimeDataClass::getBurglaryRate))
.get().getYear();
} // End getMinBurglaryRate
// getMaxLarcenyTheftRate
public int getMaxLarcenyTheftRate() {
return Arrays.stream(crimes)
.max(Comparator.comparing( CrimeDataClass::getLarcenyTheftRate))
.get().getYear();
} // End getMaxLarcenyTheftRate
// getMinLarcenyTheftRate
public int getMinLarcenyTheftRate() {
return Arrays.stream(crimes)
.min(Comparator.comparing( CrimeDataClass::getLarcenyTheftRate))
.get().getYear();
} // End getMinLarcenyTheftRate
// getMaxMotorVehicleTheftRate
public int getMaxMotorVehicleTheftRate() {
return Arrays.stream(crimes)
.max(Comparator.comparing( CrimeDataClass::getMotorVehicleTheftRate))
.get().getYear();
}
public int getMinMotorVehicleTheftRate() {
return Arrays.stream(crimes)
.min(Comparator.comparing( CrimeDataClass::getMotorVehicleTheftRate))
.get().getYear();
}
// toString
public String toString() {
return Arrays.toString(this.crimes);
}
public static void main(String[] args) {
CrimeDataArray array = new CrimeDataArray();
array.readFile();
for(CrimeDataClass crime: array.crimes) {
System.out.println(crime);
}
}
} // End Class
TestCrimeData.java
package crimedata;
import java.time.Duration;
import java.time.Instant;
import java.util.Scanner;
public class TestCrimeData {
public static void main(String[] args) {
// Start time
Instant start = Instant.now();
CrimeDataArray myCrimes = null;
if(args != null && args.length > 0) {
myCrimes = new CrimeDataArray(args[0]);
}else {
myCrimes = new CrimeDataArray();
}
myCrimes.readFile();
menu(myCrimes);
// End time
Instant end = Instant.now();
System.out.println("Elapsed time (seconds): " + Duration.between(start, end).toNanos() / 1_000_000_000.0);
} // End Main
private static void menu(CrimeDataArray crimes) {
// Interaction with data
Scanner scannerIn = new Scanner(System.in);
System.out.println("********** Welcome to the US Crime Statistical Application **************************");
while (true) {
System.out.println("");
System.out.println("\nEnter the number of the question you want answered. Enter ‘Q’ to quit the program.\n"
+ "\n1. What were the percentages in population growth for each consecutive year from 1994 – 2013?"
+ "\n2. What year had the highest Violent crime rate?"
+ "\n3. What year had the lowest Violent crime rate?"
+ "\n4. What year had the highest Murder rate?" + "\n5. What year had the lowest Murder rate?"
+ "\n6. What year had the highest Robbery rate?" + "\n7. What year had the lowest Robbery rate?"
+ "\n8. What year had the highest Rape rate?" + "\n9. What year had the lowest Rape rate?"
+ "\n10. What year had the highest Aggravated Assault rate?"
+ "\n11. What year had the lowest Aggravated Assault rate?"
+ "\n12. What year had the highest Property crime rate?"
+ "\n13. What year had the lowest Property crime rate?"
+ "\n14. What year had the highest Burglary rate?"
+ "\n15. What year had the lowest Burglary rate?"
+ "\n16. What year had the highest Larceny theft rate?"
+ "\n17. What year had the lowest Larceny theft rate?"
+ "\n18. What year had the highest Motor Vehicle theft rate?"
+ "\n19. What year had the lowest Motor Vehicle theft rate?"
+ "\n20. What are the total crimes commited for each year from 1994 – 2013?" + "\nQ. Quit program."
+ "\nEnter your selection:");
// Read next line
String selection = scannerIn.nextLine().trim().toUpperCase();
if (selection.equalsIgnoreCase("Q")) {
System.out.println("Thank you for trying the US Crimes Statistics Program.");
// End loop when Q is entered
break;
} // End If
else {
switch (selection) {
case "1":
crimes.getPopulationGrowth();
break;
case "2":
System.out.printf("The Violent Crime rate was highest in %d", crimes.getMaxViolentCrimeRate());
break;
case "3":
System.out.printf("The Violent Crime rate was lowest in %d", crimes.getMinViolentCrimeRate());
break;
case "4":
System.out.printf("The Murder rate was highest in %d", crimes.getMaxMurderRate());
break;
case "5":
System.out.printf("The Murder rate was lowest in %d", crimes.getMinMurderRate());
break;
case "6":
System.out.printf("The Rape rate was highest in %d", crimes.getMaxRapeRate());
break;
case "7":
System.out.printf("The Rape rate was lowest in %d", crimes.getMinRapeRate());
break;
case "8":
System.out.printf("The Robbery rate was highest in %d", crimes.getMaxRobberyRate());
break;
case "9":
System.out.printf("The Robbery rate was lowest in %d", crimes.getMinRobberyRate());
break;
case "10":
System.out.printf("The Aggravated Assault rate was highest in %d",
crimes.getMaxAggravatedAssaultRate());
break;
case "11":
System.out.printf("The Aggravated Assault rate was lowest in %d",
crimes.getMinAggravatedAssaultRate());
break;
case "12":
System.out.printf("The Aggravated Assault rate was highest in %d",
crimes.getMaxPropertyCrimeRate());
break;
case "13":
System.out.printf("The Aggravated Assault rate was lowest in %d", crimes.getMinPropertyCrimeRate());
break;
case "14":
System.out.printf("The Burglary rate was highest in %d", crimes.getMaxBurglaryRate());
break;
case "15":
System.out.printf("The Burglary rate was lowest in %d", crimes.getMinBurglaryRate());
break;
case "16":
System.out.printf("The Larceny theft was rate was highest in %d", crimes.getMaxLarcenyTheftRate());
break;
case "17":
System.out.printf("The Larceny theft was lowest in %d", crimes.getMinLarcenyTheftRate());
break;
case "18":
System.out.printf("The Motor Vehicle theft was rate was highest in %d",
crimes.getMaxMotorVehicleTheftRate());
break;
case "19":
System.out.printf("The Motor Vehicle theft was lowest in %d", crimes.getMinMotorVehicleTheftRate());
case "20":
System.out.println(crimes.toString());
break;
default:
System.out.println("Invalid selection. Please try again.");
break;
} // End Switch
} // End Else
} // End While
scannerIn.close();
} // End Menu
} // End Class