In the example code above I am reading each line of the text file (and assuming one name per line) adding each name followed by a comma (as delimiter). You can see this in the
while loop:
After the
while loop the String
names holds all the names read from the file, separated by commas (that I added as I gathered the names). Then I remove the last (trailing) comma in preparation for converting the string to an array.
You can find the
split method in the String
api. It creates and returns an array of strings demarked by the delimeter specified in the argument ("," here).
I added some
System.out.println statements to
readFile to show what
names looks like during the process. The output of names at the end is the same as the text file that contains the names (one name per line).