-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJiake.java
More file actions
173 lines (164 loc) · 6.45 KB
/
Jiake.java
File metadata and controls
173 lines (164 loc) · 6.45 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// package demo;
// import java.io.File;
// import java.util.Scanner;
// import java.io.FileNotFoundException;
// import java.util.HashMap;
// import java.util.Iterator;
// import java.util.Set;
// public class Jiake {
// public static <type> void main (String[] args) throws FileNotFoundException {
// File file=new File("Test.txt"); //读取文件
// if(!file.exists()){
// System.out.println("file doesn't exist");
// return;
// }
// Scanner scanner=new Scanner(file);
// HashMap<String,Integer> hashMap=new HashMap<String,Integer>();
// System.out.println("-----------------------------------");
// while(scanner.hasNextLine()) {
// String line=scanner.nextLine();
// // if (line=="")
// // {
// // continue;
// // }
// //System.out.println(line);
// String[] lineWords=line.split("\\W+" ); //导入文章,但是被注释掉了
// // for(int i=0;i<lineWords.length;i++) {
// // if(lineWords[i] == "\f"){
// // i--;
// // }
// // }
// lineWords[i] = lineWords[i].toLowerCase();
// System.out.print(lineWords[i] + " | ");
// }
// Set<String> wordSet=hashMap.keySet();
// for(int i=0;i<lineWords.length;i++) {
// if(wordSet.contains(lineWords[i])) {
// Integer number=hashMap.get(lineWords[i]);
// number++;
// hashMap.put(lineWords[i], number);
// }
// else {
// hashMap.put(lineWords[i], 1);
// }
// }
// }
// System.out.println("------------------------------");
// Iterator<String> iterator=hashMap.keySet().iterator();
// int max=0;
// int ot = 0;
// int count = 0;
// //String[] onetimeString = new String[lineWords.length];
// String minword=null;
// String maxword=null;
// while(iterator.hasNext()){
// String word=iterator.next();
// //System.out.printf("%-12s %d\n",word,hashMap.get(word));
// if(hashMap.get(word)>max) {
// max=hashMap.get(word);
// maxword=word;
// }
// if(hashMap.get(word) == 1) {
// // onetimeString[count]=word;
// // ot++;
// // count++;
// System.out.print(word+ " ");
// }
// // if(hashMap.get(word)<min) {
// // min=hashMap.get(word);
// // minword=word;
// // }
// }
// System.out.println();
// System.out.print("\""+maxword+"\""+" is the most frequent word, appeared ");
// System.out.println(""+max+" times");
// // for (int i = 0; i<=ot; i++){
// // System.out.println(onetimeString[i]);
// // }
// // System.out.println(maxword);
// // System.out.println(""+max+"");
// // System.out.println(""+minword);
// // System.out.println(" "+min+"");
// }
// }
package demo;
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class Jiake {
public static <type> void main (String[] args) throws FileNotFoundException {
File file=new File("Test.txt"); //读取文件
if(!file.exists()){
System.out.println("file doesn't exist");
return;
}
Scanner scanner=new Scanner(file);
HashMap<String,Integer> hashMap=new HashMap<String,Integer>();
System.out.println("-----------------------------------");
while(scanner.hasNext()) {
String line=scanner.next();
//scanner.nextline();
// while(line=="\r"){
// System.out.println("-----------------------------------");
// line=scanner.next();
// continue;
// }
//System.out.print(line);
String[] lineWords=line.split("\\W+"); //导入文章,但是被注释掉了
for(int i=0;i<lineWords.length;i++) {
lineWords[i] = lineWords[i].toLowerCase();
System.out.print(lineWords[i] + " | ");
}
Set<String> wordSet=hashMap.keySet();
for(int i=0;i<lineWords.length;i++) {
if(wordSet.contains(lineWords[i])) {
Integer number=hashMap.get(lineWords[i]);
number++;
hashMap.put(lineWords[i], number);
}
else {
hashMap.put(lineWords[i], 1);
}
}
}
System.out.println("------------------------------");
Iterator<String> iterator=hashMap.keySet().iterator();
int max=0;
int ot = 0;
int count = 0;
//String[] onetimeString = new String[lineWords.length];
String minword=null;
String maxword=null;
while(iterator.hasNext()){
String word=iterator.next();
//System.out.printf("%-12s %d\n",word,hashMap.get(word));
if(hashMap.get(word)>max) {
max=hashMap.get(word);
maxword=word;
}
if(hashMap.get(word) == 1) {
// onetimeString[count]=word;
// ot++;
// count++;
System.out.print(word+ " ");
}
// if(hashMap.get(word)<min) {
// min=hashMap.get(word);
// minword=word;
// }
}
System.out.println();
System.out.print("\""+maxword+"\""+" is the most frequent word, appeared ");
System.out.println(""+max+" times");
// for (int i = 0; i<=ot; i++){
// System.out.println(onetimeString[i]);
// }
// System.out.println(maxword);
// System.out.println(""+max+"");
// System.out.println(""+minword);
// System.out.println(" "+min+"");
}
}