Created
October 13, 2015 17:00
-
-
Save Nikamura/6d823425a6d2308406fa to your computer and use it in GitHub Desktop.
This file contains hidden or 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.*; | |
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(); | |
} | |
} | |
} |
This file contains hidden or 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
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