Skip to content

Instantly share code, notes, and snippets.

@t0dd
Created November 8, 2016 03:55
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 t0dd/693b754235e47cef54bf3d78705c9331 to your computer and use it in GitHub Desktop.
Save t0dd/693b754235e47cef54bf3d78705c9331 to your computer and use it in GitHub Desktop.
public class overFlow {
public static void main(String[] args) throws FileNotFoundException, IOException {
Map m1 = new HashMap();
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
String[] words = line.split(" ");//those are your words
for (int i = 0; i < words.length; i++) {
if (m1.get(words[i]) == null) {
m1.put(words[i], 1);
} else {
int newValue = Integer.valueOf(String.valueOf(m1.get(words[i])));
newValue++;
m1.put(words[i], newValue);
}
}
sb.append(System.lineSeparator());
line = br.readLine();
}
}
Map<String, String> sorted = new TreeMap<String, String>(m1);
for (Object key : sorted.keySet()) {
System.out.println("Word: " + key + "\tCounts: " + m1.get(key));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment