-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion2.java
More file actions
41 lines (37 loc) · 1.34 KB
/
Question2.java
File metadata and controls
41 lines (37 loc) · 1.34 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package part3_2;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
/**
*
* @author Ikhlas Kamel
*/
public class Question2 {
public static void main(String[] args) throws FileNotFoundException {
TreeMap<Character, Integer> hashMap = new TreeMap<Character, Integer>();
File file = new File("C:\\Users\\Ikhlas Kamel\\Desktop\\Texts.txt");
Scanner scanner = new Scanner(file,"utf-8");
while (scanner.hasNext()) {
char[] chars = scanner.nextLine().toLowerCase().toCharArray();
for (Character c : chars) {
if(!Character.isLetter(c)){
continue;
}
else if (hashMap.containsKey(c)) {
hashMap.put(c, hashMap.get(c) + 1);
} else {
hashMap.put(c, 1);
}
}
}
for (Map.Entry<Character, Integer> entry : hashMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}}
}