
/**
 * External text file reader and processor. Quickly get a text file
 * into an ArrayList for analysis.
 * External text file must be in same folder as this file (or 
 * current filepath).
 *
 * @author Richard White
 * @version 2023-06-01
 */

import java.util.ArrayList;
import java.util.Scanner;    // reads input from stream
import java.io.File;         // accesses external file
import java.io.FileNotFoundException;  // required in case file isn't found

public class QuickDirtyReader
{
    public static void main(String[] args) throws FileNotFoundException
    {
        ArrayList<String> lines = new ArrayList<String>();
        // Set up reference to external file
        Scanner in = new Scanner(new File("data.txt"));
        // Go through the file line by line
        while (in.hasNext())
        {
            lines.add(in.nextLine());
        }
        
        // Now do whatever you want. You can...
        
        // 1. go through the lines (printing them in this example)
        for (String line : lines)
        {
            System.out.println(line);
        }
        
        // 2. go through the items separated by commas (which may
        //    all be on one line)
        for (String word : lines.get(0).split(","))
        {
            System.out.println(word);
        }
        
        // 3. maybe they're numbers? Put the numbers into an Integer
        //    ArrayList for more analysis
        ArrayList<Integer> nums = new ArrayList<Integer>();
        for (String str : lines.get(0).split(","))
        {
            nums.add(Integer.parseInt(str)); // converts to Integer
        }
        
        // 4. Now you can do something with the numbers
        System.out.println("Adding all the numbers in the text file");
        int total = 0;
        for (int i = 0; i < nums.size(); i++)
            total += nums.get(i);
        System.out.println(total);
        
    }
}
