Lukasz
Lukasz

Reputation: 15

Can only iterate over an array or an instance of java.lang.Iterable in java

for(List<String> wlist : an.getSortedByAnQty())

I am getting the error:

Can only iterate over an array or an instance of java.lang.Iterable

What does this mean? How can I fix it?

import java.io.*;

import java.util.*;

public class Main {

  public static void main(String[] args) throws FileNotFoundException {

    String home = System.getProperty("user.home");
    String allWords = home + "/allwords.txt";
    Anagrams an = new Anagrams(allWords);
    for(List<String> wlist : an.getSortedByAnQty()) {
      System.out.println(wlist);
    }
    System.out.println("************************");
    Scanner scan = new Scanner(new File(home, "wordsToFind.txt"));
    while(scan.hasNext()) {
      System.out.println(an.getAnagramsFor(scan.next()));
    }
    scan.close();
  }
}
import java.util.Iterator;
import java.util.List;

public class Anagrams implements Iterable<List<String>>{

    public Anagrams(String allWords) {      
    }
    public Object getSortedByAnQty() {          
        return null;
    }
    public char[] getAnagramsFor(String next) {             
        return null;
    }
    @Override
    public Iterator<List<String>> iterator() {              
        return null;
    }
} 

Upvotes: 1

Views: 649

Answers (1)

xtratic
xtratic

Reputation: 4699

In the code for(List<String> wlist : an.getSortedByAnQty())...

an.getSortedByAnQty() returns an Object (null) which is not an instance of Iterable<List<String>>.

What you want is probably just this for(List<String> wlist : an) since an is of type Anagrams which implements Iterable<List<String>>

Upvotes: 2

Related Questions