-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem13.java
More file actions
54 lines (52 loc) · 1.05 KB
/
Problem13.java
File metadata and controls
54 lines (52 loc) · 1.05 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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
class Text{
private String str;
public Text() {
str = new String();
}
boolean readTextFromFile(String f){
BufferedReader br;
try {
br = new BufferedReader(new FileReader(f));
}catch (FileNotFoundException e) {
System.out.println("Input file not found");
return false;
}
while(true) {
try {
String line = br.readLine();
if(line == null) break;
str += line;
} catch(IOException e) {
e.printStackTrace();
}
}
try {
br.close();
}catch(IOException e) {
e.printStackTrace();
}
return true;
}
int countChar(char a) {
int count = 0;
for(int i = 0; i < str.length(); i++) {
if(str.toLowerCase().charAt(i) == a)
count++;
}
return count;
}
}
public class Problem13 {
public static void main(String[] args) {
Text t = new Text();
if(t.readTextFromFile("input_prob13.txt")) {
for(char c = 'a'; c <= 'z'; c++) {
System.out.println(c+ ": "+ t.countChar(c));
}
}
}
}