We will read in the csv file that can be found here: https://github.com/washingt
ID: 3808420 • Letter: W
Question
We will read in the csv file that can be found here: https://github.com/washingtonpost/data-police-shootings The Washington Post is compiling a database of every fatal shooting in the United States by a police officer in the line of duty in 2015 and 2016. We will make an object to represent a record in this file and then read the data into an array of these objects. We can then answer questions regarding this data: 1. What was the percentage of records where the person was unarmed? 2. What percentage was Male? 3. How many shootings happened overall? How many were in California? Make a method to answer each of these questions.Explanation / Answer
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class csvfileread {
public static void main(String[] args) {
try {
URL url = new URL("https://github.com/washingtonpost/data-police-shootings/blob/master/fatal-police-shootings-
data.csv" );
URLConnection uc = url.openConnection();
outStreamReader inStream = new InputStreamReader(uc.getInputStream());
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
lineNumberReader.skip(Long.MAX_VALUE);
int lines = lineNumberReader.getLineNumber();
string[] col = line.split(cvsSplitBy);
int id = Integer.parseInt(col[0]);
string name = col[1];
string date = col[2];
string manner_of_death = col[3];
string armed = col[4];
int age = Integer.parseInt(col[5]);
string gender = col[6];
string race = col[7];
string city = col[8];
string state = col[9];
string armd = "unarmed"; int count_unarmed, percentage_unarmed;
string mal = "M"; int count_of_male, percentage_male;
string manner_death = "shot"; int death_mannerc_count;
string cty = "California"; int california_count;
"if(armed == armd)
{
count_unarmed++;
}
if(gender == mal)
{
count_of_male++;
}
if(manner_of_death ==manner_death)
{
death_mannerc_count++;
if(city == cty)
{
california_count++;
;
}}
percentage_male = (count_of_male / lines) * 100;
percentage_unarmed = (count_unarmed / lines) )100;
System.out.println("Percentage of records with unarmed" + percentage_unarmed);
System.out.println("Percentage of male records" + percentage_male);
System.out.println("Total shooting happened" + death_mannerc_count);
System.out.println("Total shooting happened" + death_mannerc_count);
System.out.println("count of shooting happend in California" + california_count);}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.