1
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;
    }
} 
2
  • So what is not clear? What an.getSortedByAnQty() returns? Commented Apr 11, 2018 at 20:09
  • an.getSortedByAnQty() returns an Object (null) which is not Iterable<List<String>> what you mean is just this for(List<String> wlist : an) Commented Apr 11, 2018 at 20:10

1 Answer 1

2

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>>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.