The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 337 Weeks Ago, 4 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
I am trying to compile this project in Netbeans and getting the following error:
run:
Welcome to US Crime Stats Application
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at crimes.TestCrimes.main(TestCrimes.java:204)
C:UserskarisAppDataLocalNetBeansCache8.2executor-snippetsrun.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
I have attached my java and data files as well as the assignment. Thanks for your assistance./*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package crimes;
/*
* File: Crimes.java
* Author: Karisa Frazier
* Date: May 5, 2017
* Purpose: This program will read a file containing data related to the US.
*Crime statistics from 1994-2013.
*/
//USCrimeApplication.java
public class Crimes
{
//Declare variables.
public double CrimeYear;
public double USPopulation;
public double ViolentCrime;
public double ViolentCrimeRate;
public double MurderCrime;
public double MurderCrimeRate;
public double RapeCrime;
public double RapeCrimeRate;
public double RobberyCrime;
public double RobberyCrimeRate;
public double AssaultCrime;
public double AssaultCrimeRate;
public double PropertyCrime;
public double PropertyCrimeRate;
public double BurglaryCrime;
public double BurglaryCrimeRate;
public double LarcenyTheft;
public double LarcenyTheftRate;
public double VehicleTheft;
public double VehicleTheftRate;
//Constructor.
public Crimes( String[] info)
{
//Constructor stub.
CrimeYear = Double.parseDouble(info[0]);
USPopulation = Double.parseDouble(info[1]);
ViolentCrime = Double.parseDouble(info[2]);
ViolentCrimeRate = Double.parseDouble(info[3]);
MurderCrime = Double.parseDouble(info[4]);
MurderCrimeRate = Double.parseDouble(info[5]);
RapeCrime = Double.parseDouble(info[6]);
RapeCrimeRate = Double.parseDouble(info[7]);
RobberyCrime = Double.parseDouble(info[8]);
RobberyCrimeRate = Double.parseDouble(info[9]);
AssaultCrime = Double.parseDouble(info[10]);
AssaultCrimeRate = Double.parseDouble(info[11]);
PropertyCrime = Double.parseDouble(info[12]);
PropertyCrimeRate = Double.parseDouble(info[13]);
BurglaryCrime = Double.parseDouble(info[14]);
BurglaryCrimeRate = Double.parseDouble(info[15]);
LarcenyTheft = Double.parseDouble(info[16]);
LarcenyTheftRate = Double.parseDouble(info[17]);
VehicleTheft = Double.parseDouble(info[18]);
VehicleTheftRate = Double.parseDouble(info[19]);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package crimes;
/**
*
* @author karis
*/
//DisplayMenu.java
//Class name.
public class DisplayCrimes
{
//Main().
public static void printMenu()
{
//Display menu().
//Prompt the user.
System.out.println("Enter the number of the question you want answered. Enter 'Q' to quit the program :");
//One to display population growth.
System.out.println("1. What were the percentages in population growth for each consecutive year from 1994 - 2013?");
//Two to display highest murder rate.
System.out.println("2. What year was the Murder rate the highest?");
//Three to display lowest murder rate.
System.out.println("3. What year was the Murder rate the lowest?");
//Four to display highest robbery crime rate.
System.out.println("4. What year was the Robbery Crime rate the highest?");
//Five to display lowest robbery crime rate.
System.out.println("5. What year was the Robbery Crime rate the lowest?");
//Six to display Motor vehicle theft percentage change.
System.out.println("6. What was the total percentage change in Motor Vehicle Theft between 1998 and 2012?");
//Seven to display first unique.
System.out.println("7. What was [enter your first unique statistic here]?");
//Eight to display second unique.
System.out.println("8. What was [enter your second unique statistic here]?");
//Enter q to quit.
System.out.println("Q. Quit the program");
}
}/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package crimes;
/**
*
* @author karis
*/
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
//Class.
public class TestCrimes
{
//Method populationGrowth().
public static void populationGrowth(ArrayList aList)
{
//Display message.
System.out.println("The percentages in population growth for each consecutive year from 1994 - 2013");
//Loop().
for (int idx = 0; idx < aList.size(); idx++)
{
//Create instances for Crimes class.
Crimes ob1 = (Crimes) aList.get(idx);
Crimes ob2 = null;
//Condition check.
if (ob1.CrimeYear < 2013)
{
ob2 = (Crimes) aList.get(idx + 1);
//Display message.
System.out.println(" Duration : " + ob1.CrimeYear + "-" + ob2.CrimeYear + " --> " + (((ob2.USPopulation - ob1.USPopulation) / ob1.USPopulation) * 100) + "%");
}
}
}
//Method MotorVehicleTheft().
public static void MotorVehicleTheft(ArrayList aList)
{
//Display message.
System.out.println("The total percentage change in Motor Vehicle Theft between 1998 and 2012");
//Loop().
for (int idx = 0; idx < aList.size(); idx++)
{
//Create instances for Crimes class.
Crimes ob1 = (Crimes) aList.get(idx);
Crimes ob2 = null;
//Condition check.
if (ob1.CrimeYear > 1998 && ob1.CrimeYear < 2013)
{
ob2 = (Crimes) aList.get(idx + 1);
//Display message.
System.out.println(" Duration : " + ob1.CrimeYear + "-" + ob2.CrimeYear + " --> " + (((ob2.VehicleTheft - ob1.VehicleTheft) / ob1.VehicleTheft) * 100) + "%");
}
}
}
//Method MurderRateTheHighest().
public static void MurderRateTheHighest(ArrayList aList)
{
//Declare the needed variables.
double murderRateMax = Double.MIN_VALUE;
int crimeYear = 0;
//Loop().
for (int idx = 0; idx < aList.size(); idx++)
{
//Create instance for Crimes class.
Crimes ob1 = (Crimes) aList.get(idx);
//Condition check.
if (murderRateMax < ob1.MurderCrimeRate)
{
murderRateMax = ob1.MurderCrimeRate;
crimeYear = (int) ob1.CrimeYear;
}
}
//Display message.
System.out.println("Murder rate the highest : ---> " + crimeYear);
}
//Method MurderRateTheLowest().
public static void MurderRateTheLowest(ArrayList aList)
{
//Declare the needed variables.
double minMurderrate = Double.MAX_VALUE;
int crimeYear = 0;
//Loop().
for (int idx = 0; idx < aList.size(); idx++)
{
//Create instance for Crimes class.
Crimes ob1 = (Crimes) aList.get(idx);
//Condition check.
if (minMurderrate > ob1.MurderCrimeRate)
{
minMurderrate = ob1.MurderCrimeRate;
crimeYear = (int) ob1.CrimeYear;
}
}
//Display message.
System.out.println("Murder rate the LOWEST : ---> " + crimeYear);
}
//Method RobberyRateHighest().
public static void RobberyRateHighest(ArrayList aList)
{
//Declare the needed variables.
double robberyRateMax = Double.MIN_VALUE;
int crimeYear = 0;
//Loop().
for (int idx = 0; idx < aList.size(); idx++)
{
//Create instance for Crimes class.
Crimes ob1 = (Crimes) aList.get(idx);
//Condition check.
if (robberyRateMax < ob1.RobberyCrimeRate)
{
robberyRateMax = ob1.RobberyCrimeRate;
crimeYear = (int) ob1.CrimeYear;
}
}
//Display message.
System.out.println("RobberyCrimeRate the highest : ---> " + crimeYear);
}
//Method RobberyRateLowest().
public static void RobberyRateLowest(ArrayList aList)
{
//Declare the needed variables.
double robberyRateMin = Double.MAX_VALUE;
int crimeYear = 0;
//Loop().
for (int idx = 0; idx < aList.size(); idx++)
{
//Create instance for Crimes class.
Crimes ob1 = (Crimes) aList.get(idx);
//Condition check.
if (robberyRateMin > ob1.RobberyCrimeRate)
{
robberyRateMin = ob1.RobberyCrimeRate;
crimeYear = (int) ob1.CrimeYear;
}
}
//Display message.
System.out.println("RobberyCrimeRate the LOWEST : ---> " + crimeYear);
}
//Method saveToCrimeObjin().
public static Crimes saveToCrimeObjin(String csvLine)
{
//Declare the needed variable.
String[] info = csvLine.split(",");
//Create instance for Crimes class.
Crimes ob1 = new Crimes(info);
//Return.
return ob1;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
//Main().
public static void main(String[] args)
{
long startTime = System.currentTimeMillis();
System.out.println("Welcome to US Crime Stats Application");
String inputFile = args[0];
//Try block.
try
{
//Create instances.
ArrayList aList = new ArrayList<>();
FileInputStream fistream = new FileInputStream(inputFile);
try (DataInputStream dataIn = new DataInputStream(fistream)) {
BufferedReader buffRd = new BufferedReader(new InputStreamReader(dataIn));
//Declare variable.
String stringLine;
//Loop().
while ((stringLine = buffRd.readLine()) != null) {
aList.add(saveToCrimeObjin(stringLine));
}
//Close the data input stream.
}
//Create instance.
Scanner scObj = new Scanner(System.in);
//Declare variable.
char switchChar = '\0';
//Loop().
while (switchChar != 'Q')
{
//Function call().
DisplayCrimes.printMenu();
switchChar = scObj.next().charAt(0);
//Switch case.
switch (switchChar)
{
//Case one.
case '1':
populationGrowth(aList);
break;
//Case two.
case '2':
MurderRateTheHighest(aList);
break;
//Case three.
case '3':
MurderRateTheLowest(aList);
break;
//Case four.
case '4':
RobberyRateHighest(aList);
break;
//Case five.
case '5':
RobberyRateLowest(aList);
break;
//Case six.
case '6':
MotorVehicleTheft(aList);
break;
//Case seven.
case '7': break;
//Case eight.
case '8': break;
//Case quit.
case 'Q':
//Declare variable.
long endTime = System.currentTimeMillis();
//Display message.
System.out.println("Thank you for trying the US Crimes Statistics Program.");
//Display time elapsed.
System.out.println("Elapsed time : " + (endTime - startTime) / 100);
break;
//Default case.
default:
//Display message.
System.out.println("Not a valid selection");
}
}
}
//Catch block().
catch (IOException e){}
}
}
----------- ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly