Created
November 8, 2016 03:55
-
-
Save t0dd/693b754235e47cef54bf3d78705c9331 to your computer and use it in GitHub Desktop.
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
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