0
votes

This is my first ever question, apologies for any mistakes I make while asking for help!

I am trying to sort the 5 most repeated words from a text file in Processing and I'm confused about this error I keep getting. I'm new to Java and after searching the internet, any changes I make are not appear to be helping. What do I need to do to amend to fix the issue?

Here is the code in question -

import java.util.Iterator;

import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.Multiset;
import com.google.common.collect.Multisets;

void setup() {
    size(800, 480);
    smooth();

    String[] data = loadStrings("data/data.txt"); 

    ImmutableMultiset<String> myMultiset = ImmutableMultiset.copyOf(data);
    top = Multisets.copyHighestCountFirst(myMultiset);
}

Iterator it = top.entrySet().iterator();

for (int i = 0; (i < 5) && it.hasNext(); i++) {
    Multiset.Entry entry = (Multiset.Entry) it.next();

    String word = (String) entry.getElement();
    int count = entry.getCount();

    System.out.println(word + " -> " + count);
}

Thank you kindly in advance for the help!!

2
In Java, everything has to be outside a class (except classes (and interfaces and enums and annotations, which are actually all types of classes)). - user253751
@immibis This is Processing, not straight Java. The only problem here is an extra curly brace. - Kevin Workman

2 Answers

1
votes

I edited to fix your indentation, and you appear to have an extra brace on the last line. If you copied the for-loop from inside a function, and accidentally copied the function's closing brace, please show us the rest of the function.

0
votes
top = Multisets.copyHighestCountFirst(myMultiset);
}

It seems like this brace here isn't right.

Do you intend to end your "setup" method here? If so you can't have things like sysout and for's not in a method, as well as there being an extra brace at the end.

Try putting the rest of your code in a method like so:

void iteratorMethod(){
    Iterator it = top.entrySet().iterator();

    for (int i = 0; (i < 5) && it.hasNext(); i++) {
        Multiset.Entry entry = (Multiset.Entry) it.next();

        String word = (String) entry.getElement();
        int count = entry.getCount();

        System.out.println(word + " -> " + count);
    }
}

Note that you will have to call iteratorMethod() in the last line of your setup method (assuming that's what you want to do).