FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-basics/java-programs/MostRepeatedWord.java at master · CodeForHunger/java-basics · GitHub
CodeForHunger
/
java-basics
Public
forked from
learning-zone/java-basics
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java-basics
/
java-programs
/
MostRepeatedWord.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
52 lines (46 loc) · 1.16 KB
Breadcrumbs
java-basics
/
java-programs
/
MostRepeatedWord.java
Copy path
File metadata and controls
52 lines (46 loc) · 1.16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
misc
;
import
java
.
io
.
BufferedReader
;
import
java
.
io
.
FileNotFoundException
;
import
java
.
io
.
FileReader
;
import
java
.
io
.
IOException
;
import
java
.
util
.
ArrayList
;
public
class
MostRepeatedWord
{
public
static
void
main
(
String
[]
args
) {
String
line
,
word
=
""
;
int
count
=
0
,
maxCount
=
0
;
ArrayList
<
String
>
words
=
new
ArrayList
<
String
>();
// Opens file in read mode
FileReader
file
;
try
{
file
=
new
FileReader
(
"file.txt"
);
BufferedReader
br
=
new
BufferedReader
(
file
);
// Reads each line
try
{
while
((
line
=
br
.
readLine
()) !=
null
) {
String
string
[] =
line
.
toLowerCase
().
split
(
"([,.
\\
s]+)"
);
for
(
String
s
:
string
) {
words
.
add
(
s
);
}
}
}
catch
(
IOException
e
) {
e
.
printStackTrace
();
}
}
catch
(
FileNotFoundException
e
) {
e
.
printStackTrace
();
}
// Determine the most repeated word in a file
for
(
int
i
=
0
;
i
<
words
.
size
();
i
++) {
count
=
1
;
for
(
int
j
=
i
+
1
;
j
<
words
.
size
();
j
++) {
if
(
words
.
get
(
i
).
equals
(
words
.
get
(
j
))) {
count
++;
}
}
if
(
count
>
maxCount
) {
maxCount
=
count
;
word
=
words
.
get
(
i
);
}
}
System
.
out
.
println
(
"Most Repeated words: "
+
word
);
}
}
Back
|
FazBrowse Home
|
New Git URL