I have this java method which returns an ArrayList, but I want to return an Array of Strings. The method reads the file words.txt (contains all words with a word on each line), and I want to store those words into an Array of Strings.
Heres the code I already have:
public static ArrayList<String> readFile(){
File myFile=new File("./src/folder/words.txt");
Scanner s1=null;
//Creates ArrayList to store each String aux
ArrayList<String> myWords = new ArrayList<String>();
try {
s1 = new Scanner(myFile);
}catch (FileNotFoundException e) {
System.out.println("File not found");
e.printStackTrace();
}
while(s1.hasNext()){
String aux=s1.next();
System.out.println(aux);
}
s1.close();
return myWords;
}
Can I change this code to return a String []?