Movatterモバイル変換


[0]ホーム

URL:


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

Commit3f23972

Browse files
author
jsquared21
committed
Add Ex 21.12
1 parent50d6eac commit3f23972

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*********************************************************************************
2+
* (Name for both genders) Write a program that prompts the user to enter one of *
3+
* the filenames described in Programming Exercise 12.31 and displays the names *
4+
* that are used for both genders in the file. Use sets to store names and find *
5+
* common names in two sets. Here is a sample run: *
6+
*********************************************************************************/
1.85 KB
Binary file not shown.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*********************************************************************************
2+
* (Name for both genders) Write a program that prompts the user to enter one of *
3+
* the filenames described in Programming Exercise 12.31 and displays the names *
4+
* that are used for both genders in the file. Use sets to store names and find *
5+
* common names in two sets. Here is a sample run: *
6+
*********************************************************************************/
7+
importjava.util.*;
8+
9+
publicclassExercise_21_12 {
10+
11+
publicstaticvoidmain(String[]args) {
12+
// Create a Scanner
13+
Scannerinput =newScanner(System.in);
14+
15+
// Prompt the user to enter one of file names
16+
System.out.print("Enter a file name for baby name ranking: ");
17+
StringfileName =input.next();
18+
19+
// Create to sets
20+
Set<String>set1 =newHashSet<>();
21+
Set<String>set2 =newHashSet<>();
22+
23+
try {
24+
java.net.URLurl =newjava.net.URL(
25+
"http://www.cs.armstrong.edu/liang/data/" +fileName);
26+
27+
// Create input file from url and add names to sets
28+
ScannerinputStream =newScanner(url.openStream());
29+
while (inputStream.hasNext()) {
30+
inputStream.next();
31+
set1.add(inputStream.next());
32+
inputStream.next();
33+
set2.add(inputStream.next());
34+
inputStream.next();
35+
}
36+
}
37+
catch (java.net.MalformedURLExceptionex) {
38+
System.out.println("Invalid URL");
39+
}
40+
catch (java.io.IOExceptionex) {
41+
System.out.println("I/O Errors; no such file");
42+
}
43+
44+
// Display the names that are used for both genders
45+
set1.retainAll(set2);
46+
System.out.println(set1.size() +" names used for both genders");
47+
System.out.print("They are ");
48+
for (Stringname:set1) {
49+
System.out.print(name +" ");
50+
}
51+
System.out.println();
52+
}
53+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp