-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Poverty project #2
base: master
Are you sure you want to change the base?
Changes from all commits
96f2bb2
03b7f95
ba1263e
f708ce9
fd8daad
43e5a39
41d88af
5a2f513
5d505a4
890a11c
959cd86
5bc91c3
63637e8
aadf007
1f2c7f2
2d63317
93aeb71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
.DS_Store | ||
|
||
.metadata | ||
bin/ | ||
tmp/ | ||
*.tmp | ||
*.bak | ||
*.swp | ||
*~.nib | ||
local.properties | ||
.settings/ | ||
.loadpath | ||
.recommenders | ||
|
||
# Eclipse Core | ||
.project | ||
|
||
# External tool builders | ||
.externalToolBuilders/ | ||
|
||
# Locally stored "Eclipse launch configurations" | ||
*.launch | ||
|
||
# PyDev specific (Python IDE for Eclipse) | ||
*.pydevproject | ||
|
||
# CDT-specific (C/C++ Development Tooling) | ||
.cproject | ||
|
||
# JDT-specific (Eclipse Java Development Tools) | ||
.classpath | ||
|
||
# Java annotation processor (APT) | ||
.factorypath | ||
|
||
# PDT-specific (PHP Development Tools) | ||
.buildpath | ||
|
||
# sbteclipse plugin | ||
.target | ||
|
||
# Tern plugin | ||
.tern-project | ||
|
||
# TeXlipse plugin | ||
.texlipse | ||
|
||
# STS (Spring Tool Suite) | ||
.springBeans | ||
|
||
# Code Recommenders | ||
.recommenders/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package interactiveinfo; | ||
|
||
import java.util.Scanner; | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
|
||
public class GetPovertyInfo { | ||
|
||
public static String getMatch(String county) throws FileNotFoundException { | ||
File data = new File("support/povWA.txt"); | ||
Scanner scnr = new Scanner(data); | ||
scnr.nextLine(); // skip first line | ||
|
||
while(scnr.hasNextLine()) { | ||
String line = scnr.nextLine(); | ||
if ((county + " County").equals(line.substring(193, 238).trim()) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you adjust the substring function to ignore the "county" part and simply check if |
||
String match = line; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this extra variable needed? |
||
scnr.close(); | ||
return match; | ||
} | ||
} | ||
|
||
scnr.close(); | ||
return null; | ||
} | ||
|
||
public static void printCountyInfo(String countyRecord) { | ||
System.out.println(String.format("In %s, %s children (%s%%) are in poverty. The median household income is $%s.", countyRecord.substring(193, 239).trim(), countyRecord.substring(49, 57).trim(), countyRecord.substring(76, 80).trim(), countyRecord.substring(133, 139).trim())); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package interactiveinfo; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.util.Scanner; | ||
|
||
import org.apache.commons.lang3.text.WordUtils; | ||
|
||
public class Program { | ||
|
||
public static void main(String[] args) throws FileNotFoundException { | ||
Scanner scnr = new Scanner(System.in); | ||
System.out.println("Enter a county name in Washington (e.g., King), or type `quit` to quit: "); | ||
String input = WordUtils.capitalizeFully(scnr.nextLine().trim()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice job finding the functions that would do this for you |
||
|
||
String record = null; | ||
while (!input.equals("Quit") && !input.equals("Q")) { | ||
record = GetPovertyInfo.getMatch(input); | ||
|
||
while (record == null) { | ||
scnr = new Scanner(System.in); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could adjust to have record start out as null and put all of the code from lines 11-13 in this while look as well so that none of it is duplicated. (Though, this would change your error message for the user) |
||
System.out.println("No match found. Try again: "); | ||
input = WordUtils.capitalizeFully(scnr.nextLine().trim()); | ||
record = GetPovertyInfo.getMatch(input); | ||
} | ||
|
||
GetPovertyInfo.printCountyInfo(record); | ||
scnr = new Scanner(System.in); | ||
System.out.println("Enter another county, or type `quit` to quit: "); | ||
input = WordUtils.capitalizeFully(scnr.nextLine().trim()); | ||
} | ||
|
||
scnr.close(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package stateinfo; | ||
|
||
import java.util.Scanner; | ||
import java.io.File; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.io.FileNotFoundException; | ||
|
||
public class GetPovertyInfo { | ||
|
||
private static final Map<String, String> stateFiles; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good use of the |
||
static | ||
{ | ||
stateFiles = new HashMap<String, String>(); | ||
stateFiles.put("Washington", "support/povWA.txt"); | ||
stateFiles.put("South Dakota", "support/povSD.txt"); | ||
} | ||
|
||
public static String getMinimumChildPovertyRecord(String state) throws FileNotFoundException { | ||
File data = new File(stateFiles.get(state)); | ||
Scanner scnr = new Scanner(data); | ||
|
||
scnr.nextLine(); // skip first line | ||
|
||
String minCountyRecord = null; | ||
Double currentMin = null; | ||
|
||
while(scnr.hasNextLine()){ | ||
|
||
String line = scnr.nextLine(); | ||
|
||
if (currentMin == null) { | ||
currentMin = Double.parseDouble(line.substring(76, 80)); | ||
minCountyRecord = line; | ||
} | ||
|
||
if (Double.parseDouble(line.substring(76, 80)) < currentMin) { | ||
currentMin = Double.parseDouble(line.substring(76, 80)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like these same substring values are used many times. |
||
minCountyRecord = line; | ||
} | ||
|
||
} | ||
|
||
scnr.close(); | ||
return minCountyRecord; | ||
} | ||
|
||
public static String getMaximumChildPovertyRecord(String state) throws FileNotFoundException { | ||
File data = new File(stateFiles.get(state)); | ||
Scanner scnr = new Scanner(data); | ||
|
||
scnr.nextLine(); // skip first line | ||
|
||
String maxCountyRecord = null; | ||
Double currentMax = null; | ||
|
||
while(scnr.hasNextLine()){ | ||
|
||
String line = scnr.nextLine(); | ||
|
||
if (currentMax == null) { | ||
currentMax = Double.parseDouble(line.substring(76, 80)); | ||
maxCountyRecord = line; | ||
} | ||
|
||
if (Double.parseDouble(line.substring(76, 80)) > currentMax) { | ||
currentMax = Double.parseDouble(line.substring(76, 80)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment re: this line.substring |
||
maxCountyRecord = line; | ||
} | ||
|
||
} | ||
|
||
scnr.close(); | ||
return maxCountyRecord; | ||
} | ||
|
||
public static void printChildPovertyInfo(String state) throws FileNotFoundException { | ||
String minCountyRecord = GetPovertyInfo.getMinimumChildPovertyRecord(state); | ||
String maxCountyRecord = GetPovertyInfo.getMaximumChildPovertyRecord(state); | ||
|
||
System.out.println(String.format("In %s:", state)); | ||
System.out.println(String.format("The county with the lowest percentage of children in poverty is %s. In that county, %s children (%s%%) are in poverty. The median household income is $%s.", minCountyRecord.substring(193, 239).trim(), minCountyRecord.substring(49, 57).trim(), minCountyRecord.substring(76, 80).trim(), minCountyRecord.substring(133, 139).trim())); | ||
System.out.println(String.format("The county with the highest percentage of children in poverty is %s. In that county, %s children (%s%%) are in poverty. The median household income is $%s.", maxCountyRecord.substring(193, 239).trim(), maxCountyRecord.substring(49, 57).trim(), maxCountyRecord.substring(76, 80).trim(), maxCountyRecord.substring(133, 139).trim())); | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package stateinfo; | ||
|
||
import java.io.FileNotFoundException; | ||
|
||
public class Program { | ||
|
||
public static void main(String[] args) throws FileNotFoundException { | ||
GetPovertyInfo.printChildPovertyInfo("Washington"); | ||
GetPovertyInfo.printChildPovertyInfo("South Dakota"); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that you added the
throws
here. I would say it is more typical to put atry
andcatch
around the block of code than to put thethrows
at the method level.