Repository: jtablesaw/tablesaw
Pull request #359 (2018-08-20 15:38:37)

Use formatted strings when printing table
Thanks for contributing.

- [X] Tick to sign-off your agreement to the [Developer Certificate of Origin (DCO) 1.1](https://developercertificate.org)

## Description

Fix for https://github.com/jtablesaw/tablesaw/issues/357


Patch:
diff --git a/core/src/examples/java/tech/tablesaw/CrossTabsExample.java b/core/src/examples/java/tech/tablesaw/CrossTabsExample.java
index 1d504788..95a9bc7a 100644
--- a/core/src/examples/java/tech/tablesaw/CrossTabsExample.java
+++ b/core/src/examples/java/tech/tablesaw/CrossTabsExample.java
@@ -12,8 +12,7 @@ public class CrossTabsExample {
 
         Table table = Table.read().csv("../data/bush.csv");
         StringColumn month = table.dateColumn("date").month();
-        month.setName("month");
-        table.addColumns(month);
+        table.addColumns(month.setName("month"));
 
         // two variable counts
         Table counts = table.xTabCounts("month", "who");
diff --git a/core/src/main/java/tech/tablesaw/api/Table.java b/core/src/main/java/tech/tablesaw/api/Table.java
index b347bbfe..d624b4dd 100644
--- a/core/src/main/java/tech/tablesaw/api/Table.java
+++ b/core/src/main/java/tech/tablesaw/api/Table.java
@@ -317,19 +317,6 @@ public class Table extends Relation implements Iterable<Row> {
         return names;
     }
 
-    /**
-     * Returns a string representation of the value at the given row and column indexes
-     *
-     * @param r          the row index, 0 based
-     * @param columnName the name of the column to be returned
-     *                   <p>
-     *                   // TODO: performance would be enhanced if columns could be referenced via a hashTable
-     */
-    public String get(int r, String columnName) {
-        Column<?> column = column(columnIndex(columnName));
-        return column.getString(r);
-    }
-
     /**
      * Returns a table with the same columns as this table
      */
diff --git a/core/src/main/java/tech/tablesaw/io/string/DataFramePrinter.java b/core/src/main/java/tech/tablesaw/io/string/DataFramePrinter.java
index 5b9b8f18..43741a84 100644
--- a/core/src/main/java/tech/tablesaw/io/string/DataFramePrinter.java
+++ b/core/src/main/java/tech/tablesaw/io/string/DataFramePrinter.java
@@ -14,13 +14,13 @@
 
 package tech.tablesaw.io.string;
 
-import tech.tablesaw.table.Relation;
-import tech.tablesaw.util.StringUtils;
-
 import java.io.IOException;
 import java.io.OutputStream;
 import java.util.stream.IntStream;
 
+import tech.tablesaw.table.Relation;
+import tech.tablesaw.util.StringUtils;
+
 /**
  * A class that can pretty print a DataFrame to text for visualization in a console
  * <p>
@@ -186,7 +186,7 @@ public class DataFramePrinter {
             int i;
             for (i = 0; i < rowCount / 2; i++) {
                 for (int j = 0; j < colCount; j++) {
-                    data[i][j] = frame.getUnformatted(i, j);
+                    data[i][j] = frame.getString(i, j);
                 }
             }
             for (int j = 0; j < colCount; j++) {
@@ -194,13 +194,13 @@ public class DataFramePrinter {
             }
             for (i++; i < rowCount; i++) {
                 for (int j = 0; j < colCount; j++) {
-                    data[i][j] = frame.getUnformatted(frame.rowCount() - maxRows + i, j);
+                    data[i][j] = frame.getString(frame.rowCount() - maxRows + i, j);
                 }
             }
         } else {
             for (int i = 0; i < rowCount; i++) {
                 for (int j = 0; j < colCount; j++) {
-                    String value = frame.getUnformatted(i, j);
+                    String value = frame.getString(i, j);
                     data[i][j] = value == null ? "" : value;
                 }
             }
diff --git a/core/src/main/java/tech/tablesaw/table/Relation.java b/core/src/main/java/tech/tablesaw/table/Relation.java
index 532a1935..5f6aebc9 100644
--- a/core/src/main/java/tech/tablesaw/table/Relation.java
+++ b/core/src/main/java/tech/tablesaw/table/Relation.java
@@ -433,6 +433,31 @@ public abstract class Relation {
         return column.getUnformattedString(r);
     }
 
+    /**
+     * Returns a string representation of the value at the given row and column indexes
+     *
+     * @param r          the row index, 0 based
+     * @param columnName the name of the column to be returned
+     *                   <p>
+     *                   // TODO: performance would be enhanced if columns could be referenced via a hashTable
+     */
+    public String getString(int r, String columnName) {
+        return getString(r, columnIndex(columnName));
+    }
+
+    /**
+     * Returns a string representation of the value at the given row and column indexes
+     *
+     * @param r          the row index, 0 based
+     * @param columnName the name of the column to be returned
+     *                   <p>
+     *                   // TODO: performance would be enhanced if columns could be referenced via a hashTable
+     */
+    public String getString(int r, int columnIndex) {
+        Column<?> column = column(columnIndex);
+        return column.getString(r);
+    }
+
     public boolean containsColumn(Column<?> column) {
         return columns().contains(column);
     }


Test patch:
diff --git a/core/src/test/java/tech/tablesaw/TableFilteringTest.java b/core/src/test/java/tech/tablesaw/TableFilteringTest.java
index 258dfe62..f8e9c128 100644
--- a/core/src/test/java/tech/tablesaw/TableFilteringTest.java
+++ b/core/src/test/java/tech/tablesaw/TableFilteringTest.java
@@ -81,7 +81,7 @@ public class TableFilteringTest {
         assertEquals(10, result.rowCount());
         for (Column<?> c: result.columns()) {
             for (int r = 0; r < result.rowCount(); r++) {
-                assertEquals(table.get(r+20, c.name()), result.get(r, c.name()));
+                assertEquals(table.getString(r+20, c.name()), result.getString(r, c.name()));
             }
         }
     }
@@ -91,8 +91,8 @@ public class TableFilteringTest {
         Table result = table.rows(20, 30);
         assertEquals(2, result.rowCount());
         for (Column<?> c: result.columns()) {
-            assertEquals(table.get(20, c.name()), result.get(0, c.name()));
-            assertEquals(table.get(30, c.name()), result.get(1, c.name()));
+            assertEquals(table.getString(20, c.name()), result.getString(0, c.name()));
+            assertEquals(table.getString(30, c.name()), result.getString(1, c.name()));
         }
     }
 
@@ -113,8 +113,8 @@ public class TableFilteringTest {
         Table result = table.dropRows(20, 30);
         assertEquals(table.rowCount() - 2, result.rowCount());
         for (Column<?> c: result.columns()) {
-            assertEquals(table.get(21, c.name()), result.get(20, c.name()));
-            assertEquals(table.get(32, c.name()), result.get(30, c.name()));
+            assertEquals(table.getString(21, c.name()), result.getString(20, c.name()));
+            assertEquals(table.getString(32, c.name()), result.getString(30, c.name()));
         }
     }
 
@@ -124,7 +124,7 @@ public class TableFilteringTest {
         assertEquals(table.rowCount() - 10, result.rowCount());
         for (Column<?> c: result.columns()) {
             for (int r = 30; r < result.rowCount(); r++) {
-                assertEquals(result.get(r, c.name()), table.get(r + 10, c.name()));
+                assertEquals(result.getString(r, c.name()), table.getString(r + 10, c.name()));
             }
         }
     }
diff --git a/core/src/test/java/tech/tablesaw/api/NumberColumnTest.java b/core/src/test/java/tech/tablesaw/api/NumberColumnTest.java
index 51a9ca03..dd195387 100644
--- a/core/src/test/java/tech/tablesaw/api/NumberColumnTest.java
+++ b/core/src/test/java/tech/tablesaw/api/NumberColumnTest.java
@@ -233,7 +233,7 @@ public class NumberColumnTest {
         Selection filter = t.numberColumn("Test").isBetweenExclusive(42, 57);
         Table result = t.where(filter);
         assertEquals(1, result.rowCount());
-        assertEquals("52", result.get(0, "Test"));
+        assertEquals("52", result.getString(0, "Test"));
     }
 
     @Ignore
diff --git a/core/src/test/java/tech/tablesaw/api/QueryHelperTest.java b/core/src/test/java/tech/tablesaw/api/QueryHelperTest.java
index 6cd299c6..e4be8dd8 100644
--- a/core/src/test/java/tech/tablesaw/api/QueryHelperTest.java
+++ b/core/src/test/java/tech/tablesaw/api/QueryHelperTest.java
@@ -22,16 +22,16 @@ public class QueryHelperTest {
                                         .and(t.numberColumn("approval").isLessThan(75))
                 )
         );
-        assertTrue(result.get(0, "who").startsWith("f"));
+        assertTrue(result.getString(0, "who").startsWith("f"));
     }
 
     @Test
     public void test3() {
         Table result = t.where(t.stringColumn("who").isIn("fox"));
-        assertEquals("fox", result.get(0, "who"));
+        assertEquals("fox", result.getString(0, "who"));
 
         result = t.where(t.stringColumn("who").isNotIn("fox", "zogby"));
-        assertFalse(result.get(0, "who").startsWith("f"));
+        assertFalse(result.getString(0, "who").startsWith("f"));
     }
 
     @Test
@@ -39,6 +39,6 @@ public class QueryHelperTest {
         Table result = t.where(
                 t.stringColumn("who").startsWith("f"));
 
-        assertTrue(result.get(0, "who").startsWith("f"));
+        assertTrue(result.getString(0, "who").startsWith("f"));
     }
 }
diff --git a/core/src/test/java/tech/tablesaw/api/TimeColumnTest.java b/core/src/test/java/tech/tablesaw/api/TimeColumnTest.java
index fbed4699..38f554f7 100644
--- a/core/src/test/java/tech/tablesaw/api/TimeColumnTest.java
+++ b/core/src/test/java/tech/tablesaw/api/TimeColumnTest.java
@@ -189,10 +189,10 @@ public class TimeColumnTest {
     public void summary() {
         fillLargerColumn();
         Table t = column1.summary();
-        assertEquals("11", t.get(0, "Value"));
-        assertEquals("1", t.get(1, "Value"));
-        assertEquals("00:04:02", t.get(2, "Value"));
-        assertEquals("18:04:02", t.get(3, "Value"));
+        assertEquals("11", t.getString(0, "Value"));
+        assertEquals("1", t.getString(1, "Value"));
+        assertEquals("00:04:02", t.getString(2, "Value"));
+        assertEquals("18:04:02", t.getString(3, "Value"));
     }
 
     @Test