Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think this is pretty straight-forward:</p> <pre><code>public static void main(String... args) throws IOException { ArrayList&lt;ArrayList&lt;String&gt;&gt; rows = getRandomData(); if (rows.size() == 0) throw new RuntimeException("No rows"); // normalize data int longest = 0; for (List&lt;String&gt; row : rows) if (row.size() &gt; longest) longest = row.size(); for (List&lt;String&gt; row : rows) while (row.size() &lt; longest) row.add(""); if (longest == 0) throw new RuntimeException("No colums"); // fix special characters for (int i = 0; i &lt; rows.size(); i++) for (int j = 0; j &lt; rows.get(i).size(); j++) rows.get(i).set(j, fixSpecial(rows.get(i).get(j))); // get the maximum size of one column int[] maxColumn = new int[rows.get(0).size()]; for (int i = 0; i &lt; rows.size(); i++) for (int j = 0; j &lt; rows.get(i).size(); j++) if (maxColumn[j] &lt; rows.get(i).get(j).length()) maxColumn[j] = rows.get(i).get(j).length(); // create the format string String outFormat = ""; for (int max : maxColumn) outFormat += "%-" + (max + 1) + "s, "; outFormat = outFormat.substring(0, outFormat.length() - 2) + "\n"; // print the data for (List&lt;String&gt; row : rows) System.out.printf(outFormat, row.toArray()); } private static String fixSpecial(String s) { s = s.replaceAll("(\")", "$1$1"); if (s.contains("\n") || s.contains(",") || s.contains("\"") || s.trim().length() &lt; s.length()) { s = "\"" + s + "\""; } return s; } private static ArrayList&lt;ArrayList&lt;String&gt;&gt; getRandomData() { ArrayList&lt;ArrayList&lt;String&gt;&gt; data = new ArrayList&lt;ArrayList&lt;String&gt;&gt;(); String[] rand = { "Do", "Re", "Song", "David", "Test", "4", "Hohjoh", "a \"h\" o", "tjo,ad" }; Random r = new Random(5); for (int i = 0; i &lt; 10; i++) { ArrayList&lt;String&gt; row = new ArrayList&lt;String&gt;(); for (int j = 0; j &lt; r.nextInt(10); j++) row.add(rand[r.nextInt(rand.length)]); data.add(row); } return data; } </code></pre> <p>Output (pretty ugly since its random) (<a href="http://en.wikipedia.org/wiki/Comma-separated_values" rel="nofollow">escapes</a>):</p> <pre><code>Re , 4 , "tjo,ad" , "tjo,ad" , "tjo,ad" , "a ""h"" o" , , , Re , "a ""h"" o" , Hohjoh , "tjo,ad" , 4 4 , David , , , 4 , Test , "tjo,ad" , Hohjoh , Re Do , Hohjoh , Test , , Hohjoh , Song , , , 4 , Song , , , 4 , Do , Song , Do , Song , Test , Test , , </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload