matchRows of Table class problemm
in
Programming Questions
•
3 months ago
Hi,
I have problem iterating through String Array with table.matchRows command. I got the constructor from
here but it doesn't seem to work. The APlett says "Can only iterate over an array of an instance of jave.lnag.iteravle
Here is the code. The error occur on the line 68. I'm trying to count the repetitions of the same words in the text snatch from Reuters XML.
Please assist.
- XML xml;
- String[] news = new String[10]; // <<< initiate global arrayList instead
- ArrayList<String>wrd = new ArrayList(); // Declare Array List
- //int[] reps = new int[1000]; // nubmers 10 (the number of news) and 1000 (the number of words)
- //int[][] wordReps = new wordReps[
- String exceptionWords = "a,the,on,is,to,in,and,of,-,at,for,(Reuters),s";
- String[] junctions = split(exceptionWords, ",");
- Table table;
- void setup() {
- xml = loadXML("http://www.reuters.com/rssFeed/topNews"); // Adress of XML
- XML channel = xml.getChild("channel"); // Getting into the channel of the XML
- XML[] items = channel.getChildren("item/description"); // Gets into item and then in item gets into description
- for (int i = 0; i < items.length; i++) { // iterate through all elements "description"
- news[i]=(items[i].getContent()); // Record <XML> news into the GLOBAL <String> array
- }
- String words = join(news, ' '); // Join all elements of the array "news" into a single String
- String[] wordsArray = splitTokens(words, "', "); // Create array of words from the text "news"
- String[] wordsDoll = wordsArray;
- for (int i = 0; i < wordsArray.length; i++) { // Run through all words of <String> wordsArray
- for (int j = 0; j < junctions.length; j++) { // Compare all words of wordsArray with <String> array junctions
- if (wordsArray[i].equals(junctions[j]) == true) {
- //wrd.add((String)wordsArray[i]);
- wordsArray[i] = wordsArray[i].replace(junctions[j], "");
- } else {
- }
- }
- }
- //String[] wordsCopy = wrd.toArray(new String[wrd.size()]); // <<< Clean Up the Array before copying
- String[] wordsCopy = wordsArray;
- int[] reps = new int[wordsArray.length];
- int[][] wrdTable = new int [wordsArray.length][reps.length];
- //println(junctions);
- table = new Table();
- table.addColumn("word");
- table.addColumn("ocurance", Table.INT);
- for (int i = 0; i < wordsArray.length; i++) { // For loop to get item from wordsArray for comparisson
- TableRow newRow = table.addRow();
- newRow.setString("word", wordsArray[i]);
- for (int j = 0; j < wordsCopy.length; j++) { // For loop to run through every word in wordsCopy comparing the singel element from wordsArray
- if (wordsArray[i].equals(wordsCopy[j]) == true) {
- //reps[i] = reps[i] + 1;
- }
- }
- //wrdTable[i][j] =
- //println(wordsArray[i] + " repeated " + reps[i]);
- }
- for(int i=0; i<(table.getRowCount()); i++) {
- //println(table.getString(i, "word"));
- }
- for(int i=0; i < wordsArray.length; i++) {
- String iterable = wordsArray[i];
- for(TableRow row : table.matchRows(iterable, "word")) {
- reps[i]++;
- }
- println(reps[i]);
- }
- //println(wordsArray);
- }
1