I am trying to read a file and split all the words into separate Strings.
This is my code:
public String[] words(String fileName) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String word;
String[] words;
ArrayList<String> wordList = new ArrayList<>();
while ((word = reader.readLine()) != null){
words = word.split("\\s");
for (String string : words)
wordList.add(string);
}
return (String[]) wordList.toArray();
}
For some reason the line:
words = word.split("\\s");
is causing an error "Cannot resolve method 'split' in 'String'" but I have no idea why.