Say I have a file text.txt containing the following on a single line:
abc, def, ghi, jkl
I am trying to get a list of data within the file so myList will result in ["abc", "def", "ghi", "jkl"], but it isn't seem to be working:
String[] tokens;
try {
Scanner scanner = new Scanner("text.txt");
while (scanner.hasNext()) { tokens = scanner.nextLine().split(","); }
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(tokens.length); // Prints 1 instead of 4
And trying to print index 0-3 gives an out of bounds error (obviously because the length is just 1)
I then want to change the data in one of the index (in the array), and re-write/over-write it back to the file. I think this answer should work for that, but only after I can get my array right. https://stackoverflow.com/a/1016291/4669619
Edit: At the moment, I know my file only has 4 things separated by 3 commas, as shown in the sample text file.