Skip to content

Instantly share code, notes, and snippets.

@Nikamura
Created October 13, 2015 17:00
Show Gist options
  • Save Nikamura/6d823425a6d2308406fa to your computer and use it in GitHub Desktop.
Save Nikamura/6d823425a6d2308406fa to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.Hashtable;
public class LettersCounter {
private static File file;
private static BufferedReader reader;
private static Hashtable<String, Integer> lettersFreq = new Hashtable<String, Integer>();
public static void main(String[] args) {
try {
file = new File("text.txt");
reader = new BufferedReader(new FileReader(file));
String text;
while ((text = reader.readLine()) != null) {
int i;
for (i = 0; i < text.length(); i++ ) {
String currentChar = String.valueOf(text.charAt(i)).toLowerCase();
if (currentChar.matches("[A-Za-z]+")) {
if (lettersFreq.get(currentChar) == null) {
lettersFreq.put(currentChar, 0);
}
lettersFreq.put(currentChar, lettersFreq.get(currentChar) + 1);
}
}
}
System.out.println(lettersFreq);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
file = File.open("text.txt", "r")
letterList = {}
while (line = file.gets)
line.split('').each do |letter|
letterList[letter] ||= 0
letterList[letter] = letterList[letter] + 1
end
end
puts letterList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment