Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b6eecc4

Browse files
Chap 21, Ex: 12
1 parent d2a8a03 commit b6eecc4

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

‎ch_21/Exercise21_12.java‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package ch_21;
2+
3+
import java.io.IOException;
4+
import java.net.URL;
5+
import java.util.*;
6+
7+
/**
8+
* *21.12 (Name for both genders) Write a program that prompts the user to enter one of
9+
* the filenames described in Programming Exercise 12.31 and displays the names
10+
* that are used for both genders in the file. Use sets to store names and find common names in two sets.
11+
* Here is a sample run:
12+
* Enter a file name for baby name ranking: babynamesranking2001.txt
13+
* 69 names used for both genders
14+
* They are Tyler Ryan Christian ...
15+
*/
16+
public class Exercise21_12 {
17+
static final String BABY_NAMES_BASE_URL = "https://liveexample.pearsoncmg.com/data/";
18+
static final String START_FILE_NAME = "babynamesranking";
19+
20+
public static void main(String[] args) {
21+
Scanner in = new Scanner(System.in);
22+
System.out.print("Enter a file name for baby name ranking: ");
23+
String fileName = in.next().trim();
24+
// Validate user input
25+
if (!fileName.startsWith(START_FILE_NAME)) {
26+
System.out.println("File name must start with: " + START_FILE_NAME);
27+
System.exit(1);
28+
}
29+
// Extract year value form filename
30+
int year = Integer.parseInt(fileName.split("\\.")[0].substring(START_FILE_NAME.length()));
31+
if (year < 2001 || year > 2010) { // Validate year
32+
System.out.println("Year in file name must be > 2000 and < 2011.");
33+
System.exit(2);
34+
}
35+
// Resolve the full URL path
36+
String fullFilePath = BABY_NAMES_BASE_URL + fileName;
37+
Set<String> set1 = new HashSet<>();
38+
Set<String> set2 = new HashSet<>();
39+
try {
40+
getNamesList(set2, set1, fullFilePath); // Use url path to fetch populate Sets
41+
set1.retainAll(set2); // Retain all values in set1 that exist in set2 (Intersection)
42+
System.out.println(set1.size() + " names used for both genders");
43+
System.out.print("They are ");
44+
int perLine = 0;
45+
for (String name : set1) {
46+
if (perLine % 10 == 0 && perLine != 0) {
47+
System.out.print("\t\n"); // End line
48+
}
49+
System.out.print(name + " ");
50+
perLine++;
51+
}
52+
53+
} catch (IOException e) {
54+
System.out.println("Something went wrong reading the URL created from the filename input...");
55+
}
56+
57+
in.close();
58+
}
59+
60+
static void getNamesList(Set<String> guyNameList,
61+
Set<String> girlNameList,
62+
String urlString) throws IOException {
63+
URL url = new URL(urlString);
64+
Scanner urlScanner = new Scanner(url.openStream());
65+
while (urlScanner.hasNextLine()) {
66+
String line = urlScanner.nextLine();
67+
String[] record = line.split("\\s+"); // Split line example result: {"1","Jacob","32,550","Emily","25,057"}
68+
String guyName = record[1];
69+
String girlName = record[3];
70+
guyNameList.add(guyName);
71+
girlNameList.add(girlName);
72+
73+
}
74+
urlScanner.close();
75+
}
76+
77+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /