Created
October 23, 2015 01:15
-
-
Save grvcoelho/d6b4309a741bcc19dae5 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
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package textandfiles; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.util.StringTokenizer; | |
import java.util.Hashtable; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
/** | |
* | |
* @author grvcoelho | |
*/ | |
public class WordCounter { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
// TODO code application logic here | |
String texto = ""; | |
File file = new File("/Users/grvcoelho/NetBeansProjects/TextAndFiles/src/file.txt"); | |
StringBuilder sb = new StringBuilder(); | |
try { | |
FileInputStream fileStream = new FileInputStream(file); | |
while(fileStream.available() > 0) { | |
String word = Character.toString((char) fileStream.read()); | |
sb.append(word); | |
} | |
texto = sb.toString(); | |
} catch (IOException e) { | |
System.out.println(e.getMessage()); | |
} | |
String delimiters = " ,.?!<>(){}[]0123456790'"; | |
StringTokenizer st = new StringTokenizer(texto, delimiters); | |
Hashtable table = new Hashtable(); | |
while(st.hasMoreTokens()) { | |
String word = st.nextToken().toLowerCase(); | |
if(table.containsKey(word)) { | |
int ocurrencies = (int) table.get(word); | |
ocurrencies++; | |
table.put(word, ocurrencies); | |
} else { | |
table.put(word, 1); | |
} | |
} | |
System.out.println("Texto: " + texto); | |
System.out.println(table.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment