-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem15.java
More file actions
75 lines (70 loc) · 1.78 KB
/
Problem15.java
File metadata and controls
75 lines (70 loc) · 1.78 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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
class Item{
private String word;
private int count;
public Item(String word) {
this.word = word;
this.count = 1;
}
public String getWord() {return word;}
public int getCount() {return count;}
public void inputCount() {count++;}
public String toString() {return word +" "+count; }
public boolean equals(Object otherObj){
if(this == otherObj) return true;
if(otherObj == null) return false;
if(getClass() != otherObj.getClass()) return false;
Item other = (Item)otherObj;
return (this.word.equals(other.getWord()));
}
}
public class Problem15 {
public static class MyFileReader{
public static boolean readDataFromFile(String f, ArrayList<Item> list) {
BufferedReader br;
try {
br = new BufferedReader(new FileReader(f));
}catch (FileNotFoundException e) {
return false;
}
while(true) {
try {
String line = br.readLine();
if(line == null) break;
String word[] = line.split(" ");
for(String a : word) Add(a,list);
} catch(IOException e) {
e.printStackTrace();
}
}
try {
br.close();
}catch(IOException e) {
e.printStackTrace();
}
return true;
}
public static void Add(String a, ArrayList<Item> list) {
for(Item it : list) {
if(it.getWord().equals(a.toLowerCase())) {
it.inputCount();
return;
}
}
list.add(new Item(a.toLowerCase()));
}
}
public static void main(String[] args) {
ArrayList<Item> list = new ArrayList<>();
boolean rv = MyFileReader.readDataFromFile("input_prob15.txt", list);
if(rv == false) {
System.out.println("Input file not found.");
return;
}
for(Item it : list) System.out.println(it);
}
}