Answers for "how to read a table from text file in java"

0

how to read a table from text file in java

import java.io.File;
import java.io.FileNotFoundException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class BuildTableWithArrays {

    private static final String separator = "\\s+"; // regex for parsing lines
    private static final int rowWidth = 4;

    public static void main(String[] args) {

        Map<Integer, ArrayList<String>> columns = buildMapWithColumnArrayLists("animals.txt");
        printMap(columns);  // for demo
        // if you want actual arrays
        Map<Integer, String[]> colArrays = buildMapWithColumnArrays(columns);

    }

    public static Map<Integer, ArrayList<String>> buildMapWithColumnArrayLists(
        String fileName) {

        ArrayList<String> col0 = new ArrayList<String>();
        ArrayList<String> col1 = new ArrayList<String>();
        ArrayList<String> col2 = new ArrayList<String>();
        ArrayList<String> col3 = new ArrayList<String>();

        Map<Integer, ArrayList<String>> columns = new HashMap<Integer, ArrayList<String>>();
        columns.put(0, col0);
        columns.put(1, col1);
        columns.put(2, col2);
        columns.put(3, col3);

        File file = new File(fileName);
        try {
            Scanner input = new Scanner(file);
            while (input.hasNextLine()) {
                String[] line = input.nextLine().trim().replaceAll(separator, " ")
                    .split(separator);
                for (int i = 0; i < rowWidth; i++) {
                    if (line[i] == null) {
                        columns.get(Integer.valueOf(i)).add("null");
                    } else {
                        columns.get(Integer.valueOf(i)).add(line[i]);
                    }
                }
            }
            input.close();
        } catch (FileNotFoundException x) {
            System.out.println(x.getMessage());
        }

        return columns;
    }

    public static void printMap(Map<Integer, ArrayList<String>> columns) {

        for (int i = 0; i < rowWidth; i++) {
            System.out.println("col" + i + " #elements = "
                + columns.get(Integer.valueOf(i)).size());
            for (String s : columns.get(Integer.valueOf(i))) {
                System.out.print(s + " ");
            }
            System.out.println("\n");
        }
    }

    public static String[] convertArrayList2Array (ArrayList<String> arrayList) {

        String[] array = new String[arrayList.size()];
        array = arrayList.toArray(array);
        return array;

    }

    public static Map<Integer, String[]> buildMapWithColumnArrays(Map<Integer, ArrayList<String>> columns) {

        Map<Integer, String[]> cols = new HashMap<Integer, String[]>(); 

        for (Map.Entry<Integer, ArrayList<String>> entry : columns.entrySet()) {
            Integer key = entry.getKey();
            ArrayList<String> value = entry.getValue();
            String[] val = convertArrayList2Array(value);
            cols.put(key,val);
        }

        return cols;

    }

}
Posted by: Guest on April-27-2021

Code answers related to "how to read a table from text file in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language