Search code examples
javaloopsfor-loopprocessingmultiset

Passing a value/values outside of for loop to a variable - java :: processing


I am looking how to pass the value word and its corresponding count to be saved into a variable outside the for loop. My code is currently:

import java.util.Iterator; 
import java.util.*;


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

ImmutableMultiset<String> top = null;

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);
   }
}

I need these values to apply them to a bar chart in my draw() function.


Solution

  • You can declare the variables in your class and not locally in your enhanced loop.

    e.g.

    private String word;
    private int count;
    

    and use getters to call them

    e.g.

    public String getWord(){ return word; }