// FileArrayList.java -- read totally numeric CSV file and fill an appropriate ArrayList // with data from the file ... RJM Programming ... 2014 ... thanks to http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; import java.lang.*; public class FileArrayList { public static void main(String[] args) { BufferedReader br = null; String words[], delim = ",", fname; Scanner in = new Scanner(System.in); int icol = 0, i,j,k, loff=0; char cat; double darray[]; int iarray[]; boolean hasletters; ArrayList inumbers = new ArrayList<>(); ArrayList dnumbers = new ArrayList<>(); System.out.println("FileArrayList\n-------------\n\nEnter input numerical CSV file name "); fname = in.next(); try { String sCurrentLine; br = new BufferedReader(new FileReader(fname)); while ((sCurrentLine = br.readLine()) != null) { hasletters = false; for (i = 0; i < sCurrentLine.length(); i++) { cat = sCurrentLine.charAt(i); if (!hasletters) { hasletters = ((cat >= 'a' && cat <= 'z') || (cat >= 'A' && cat <= 'Z')); } } if (hasletters) { loff++; } else if (!hasletters) { words = sCurrentLine.split(delim); if (words.length == 1) { delim = " "; words = sCurrentLine.split(delim); } if (icol == 0) { if (sCurrentLine.indexOf(".") > 0) { darray = new double[words.length]; icol++; System.out.print("Line " + (icol+loff) + " of file " + fname + " resulted in ArrayList element " + (icol-1) + " for numbers[] of "); for (i = 0; i < words.length ; i++) { darray[i] = Double.parseDouble(words[i]); } dnumbers.add(darray); for (i = 0; i < words.length ; i++) { System.out.print(dnumbers.get(icol-1)[i] + " "); } System.out.println("."); } else { iarray = new int[words.length]; icol--; System.out.print("Line " + ((-icol)+loff) + " of file " + fname + " resulted in ArrayList element " + (-1-(icol)) + " for numbers[] of "); for (i = 0; i < words.length ; i++) { iarray[i] = Integer.parseInt(words[i]); } inumbers.add(iarray); for (i = 0; i < words.length ; i++) { System.out.print(inumbers.get(-1-(icol))[i] + " "); } System.out.println("."); } } else if (icol < 0) { iarray = new int[words.length]; icol--; System.out.print("Line " + ((-icol)+loff) + " of file " + fname + " resulted in ArrayList element " + (-1-(icol)) + " for numbers[] of "); for (i = 0; i < words.length ; i++) { iarray[i] = Integer.parseInt(words[i]); } inumbers.add(iarray); for (i = 0; i < words.length ; i++) { System.out.print(inumbers.get(-1-(icol))[i] + " "); } System.out.println("."); } else { darray = new double[words.length]; icol++; System.out.print("Line " + (icol+loff) + " of file " + fname + " resulted in ArrayList element " + (icol-1) + " for numbers[] of "); for (i = 0; i < words.length ; i++) { darray[i] = Double.parseDouble(words[i]); } dnumbers.add(darray); for (i = 0; i < words.length ; i++) { System.out.print(dnumbers.get(icol-1)[i] + " "); } System.out.println("."); } } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null)br.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }