I'm trying to split each line from a text file into cells in an Array. Can I do it with split function and not count the lines in order to create an array?
I am using Scanner for reading files from Scanner. The file contains in each line the following format: number_number I want to save number_number as a string into a cell in Array. However, I don't know how many lines in my text file could be, in order to set an array size. I dont want to useLinkedList`, please use the assumption that will not have many lines. Is it possible to read from file and save each line into cell in array? There is no problem to change the method from Scanner to another one.
The problem in my code currently that it saves only the first line in my file.
public String[] readFromFileToStringArray(String s) { Scanner scanner = null; try { scanner = new Scanner(new File(s)); } catch (IOException e) { e.printStackTrace(); } String[] fileText = null; if (scanner != null) while (scanner.hasNext()) fileText = scanner.nextLine().split("\n"); return fileText; }