Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package histogram_words;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Scanner;
- class wordInfo {
- String word;
- int occurences;
- public wordInfo(String w, int o) {
- word = w;
- occurences = o;
- }
- @Override
- public boolean equals(Object obj) {
- wordInfo o = (wordInfo) obj;
- return word.equals(o.word);
- }
- public String toString() {
- return word + ": " + occurences;
- }
- }
- public class Main {
- public static void main(String[] args) {
- ArrayList<wordInfo> words = new ArrayList<>();
- Scanner s;
- try {
- s = new Scanner(new File("short.txt"));
- System.out.println("Reading the wordlist...");
- int count = 0;
- while (s.hasNext()) {
- String nextWord = s.next();
- wordInfo nw = new wordInfo(nextWord, 1);
- int index = words.indexOf(nw);
- if (index == -1) {
- words.add(nw);
- } else {
- wordInfo w = words.get(index);
- w.occurences++;
- }
- count++;
- }
- System.out.println("Finished reading: " + words);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement