-
Notifications
You must be signed in to change notification settings - Fork 0
/
EthnicityRecorder.java
132 lines (113 loc) · 4.72 KB
/
EthnicityRecorder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import java.io.*;
import java.util.*;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSpan;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
public class EthnicityRecorder extends LineFinder{
public static void setEthnicityCount(ArrayList <Counties> counties) throws IOException{
EthnicityRecorder n = new EthnicityRecorder();
n.importEthnicityFiles(0);
//n.importEthnicityFiles(1);
//n.importEthnicityFiles(2);
//n.importEthnicityFiles(3);
//n.importEthnicityFiles(4);
//n.importEthnicityFiles(5);
try{
Scanner in = new Scanner (System.in);
recordEthnicityCount(counties, "totalCount", in);
recordEthnicityCount(counties, "American Indian", in);
recordEthnicityCount(counties, "White", in);
recordEthnicityCount(counties, "Asian", in);
recordEthnicityCount(counties, "African American", in);
recordEthnicityCount(counties, "Hispanic", in);
}
catch (NumberFormatException e){
System.err.println(e.getMessage());
}
}
public static void recordEthnicityCount(ArrayList <Counties> counties, String ethnicity, Scanner in) throws IOException{
String ethnicityCount = "C:\\Users\\hyma2\\Documents\\1st Semester Classes\\Java\\Project\\" + ethnicity + ".txt";
File eCount = new File(ethnicityCount);
for (int i = 0; i<counties.size(); i++){
String name = counties.get(i).getName();
int firstOccurence = findLineNums(name, in, eCount, 200);
String [] ethValue = returnData(firstOccurence, firstOccurence, eCount, in);
String ethCount = ethValue[0];
String newCount = ethCount.replace(",", "");
int count = Integer.parseInt(newCount);
if (ethnicity.equals("American Indian"))
counties.get(i).setAmericanIndian(count);
else if (ethnicity.equals("African American"))
counties.get(i).setAfricanAmerican(count);
else if (ethnicity.equals("White"))
counties.get(i).setWhite(count);
else if (ethnicity.equals("Asian"))
counties.get(i).setAsian(count);
else if (ethnicity.equals("Hispanic"))
counties.get(i).setHispanic(count);
else if (ethnicity.equals("totalCount"))
counties.get(i).setPopulation(count);
}
}
public static void importEthnicityFiles(int num){
//turns off html warnings
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);
java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);
//Sets html options
WebClient webClient = new WebClient();
webClient.getOptions().setCssEnabled(true);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getCookieManager().setCookiesEnabled(true);
String input = Integer.toString(num);
String ethnicity="";
switch(num){
case 0:
input = "All Races";
ethnicity = "totalCount";
break;
case 1:
ethnicity = "American Indian";
break;
case 2:
ethnicity = "Asian";
break;
case 3:
ethnicity = "African American";
break;
case 4:
ethnicity = "Hispanic";
break;
case 5:
ethnicity = "White";
break;
}
try {
// Get the first page and the submit button
final HtmlPage page1 = webClient.getPage("https://p1pe.doe.virginia.gov/apex/f?p=180:1:13504397254529:::::");
final HtmlForm form = page1.getFormByName("wwv_flow");
final HtmlButton button = (HtmlButton) page1.getElementsByTagName("button").get(4);
//Select Division
HtmlSelect division = form.getSelectByName("P1_REPORT_LEVEL");
division.getOptionByValue("Division").setSelected(true);
HtmlSelect county = form.getSelectByName("P1_DIVISION");
//Select Race
HtmlSelect race = form.getSelectByName("P1_RACE");
race.getOptionByValue(input).setSelected(true);
//Clicks submit button and displays text
final HtmlPage page3 = button.click();
String text = page3.asText();
//Reads text and saves it to a file
PrintWriter out = new PrintWriter(ethnicity + ".txt");
out.println(text);
out.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}