Type of mismatch: convert from string to list <String>
I mean the algorithm of my school class program, but also the difficulties in some of the basics, I think ...
here is my problem code:
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
String allWords = System.getProperty("user.home") + "/allwords.txt";
Anagrams an = new Anagrams(allWords);
for(List<String> wlist : an.getSortedByAnQty()) {
//[..............];
}
}
}
public class Anagrams {
List<String> myList = new ArrayList<String>();
public List<String> getSortedByAnQty() {
myList.add("aaa");
return myList;
}
}
I get "Type Mismatch: Cannot Convert From String Element Type to List" How to properly getSortedByAnQty () initialize?
+4
2 answers
an.getSortedByAnQty()
returns a List<String>
. When you iterate over this list, you get separate lines, so the extended loop should have a variable String
:
for(String str : an.getSortedByAnQty()) {
//[..............];
}
If the method main
should remain as it is, you must change getSortedByAnQty
to return List<List<String>>
.
+9