Created
May 24, 2020 17:43
-
-
Save kryvoboker/460fc67374c0d51941ab68b5cb79c817 to your computer and use it in GitHub Desktop.
HomeWork8(2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.Map; | |
/** | |
* | |
* @author kamaz | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
// TODO code application logic here | |
File file = new File("text.txt"); | |
String text = readFile(file); | |
Map<Character, Integer> map = new HashMap<Character, Integer>(); | |
for(int i = 0; i < text.length(); i++) { | |
char ch = text.charAt(i); | |
if (Character.isLetter(ch)) { | |
int count = map.get(ch) != null ? map.get(ch) : 0; | |
count++; | |
map.put(ch, count); | |
} | |
} | |
for(Iterator<Character> it = map.keySet().iterator(); it.hasNext(); ) { | |
Character key = it.next(); | |
System.out.println(key + " = " + map.get(key)); | |
} | |
} | |
public static String readFile(File file) { | |
StringBuilder sb = new StringBuilder(); | |
try (BufferedReader br = new BufferedReader(new FileReader(file))) { | |
String text = ""; | |
for (; (text = br.readLine()) != null;) { | |
sb.append(text); | |
sb.append(System.lineSeparator()); | |
} | |
} catch (IOException e) { | |
System.out.println(e); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment