Skip to content

Instantly share code, notes, and snippets.

@kryvoboker
Last active August 15, 2020 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kryvoboker/e64ff36d324f1384982c89dbbbacd881 to your computer and use it in GitHub Desktop.
Save kryvoboker/e64ff36d324f1384982c89dbbbacd881 to your computer and use it in GitHub Desktop.
HomeWork7(Generics and Collections)(3)
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
public class CheckLetters {
private char letter;
private int count;
public CheckLetters() {
}
public CheckLetters(int count, char letter) {
this.count = count;
this.letter = letter;
}
public char getLetter() {
return letter;
}
public void setLetter(char letter) {
this.letter = letter;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String readText(File file) {
StringBuffer sb = new StringBuffer();
String st = null;
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String str = "";
for (; (str = br.readLine()) != null;) {
sb.append(str);
sb.append(System.lineSeparator());
}
} catch (IOException e) {
e.printStackTrace();
}
st = sb.toString();
return st;
}
public ArrayList<?> count(File file) {
String alph = "abcdefghijklmnopqrstuvwxyz";
ArrayList<CheckLetters> list = new ArrayList<>();
String st = readText(file);
for (int i = 0; i < alph.length(); i += 1) {
letter = alph.charAt(i);
char[] a = st.toCharArray();
for (int j = 0; j < st.length(); j += 1) {
if (Character.isLetter(a[j])) {
if (a[j] == letter) {
count += 1;
}
}
}
list.add(new CheckLetters(getCount(), letter));
count = 0;
}
Collections.sort(list, (CheckLetters clOne, CheckLetters clTwo) -> clOne.count > clTwo.count ? -1 : (clOne.count < clTwo.count) ? 1 : 0);
return list;
}
@Override
public String toString() {
return "CheckLetters{" + count + " = " + letter + '}' + "\n";
}
}
import java.io.File;
public class Main {
public static void main(String[] args) {
CheckLetters cl = new CheckLetters();
File text = new File("a.txt");
System.out.println(cl.count(text));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment