Results 1 to 5 of 5
- 04-13-2014, 11:09 PM #1
Member
- Join Date
- Mar 2014
- Posts
- 18
- Rep Power
- 0
Formatting tables for console output
I am writing a class which formats console output as a table. It displays the type of an entry, the name of an entry, and the data for an entry. I am stuck on a required for loop which appends a tab character to a string, for formatting it as a table. It doesn't seem to be adding any of the tabs and I can't tell what I've done wrong. As far as I can tell the contents of the loop are never reached, and I can't make sense of it.
The ConsoleWriter class that contains the code that is in error...
Java Code:package frontend; public class ConsoleWriter { private static String tabbedData(String data, int min) { System.out.println(); //for debug int tabcount = 0; int len=8*min; while (data.length() <= len) { tabcount = len / 8; if (len > 0) len = len - 8; else { len = 1; break; } } System.out.println("debug: tabcount=" + tabcount); System.out.print("debug adding tabs... "); for (int x=0; x == tabcount; x++) { System.out.print("tab "); data = data + "\t"; } System.out.println(); //for debug return data; } public static void dataHead() { System.out.println("TYPE\tNAME\t\t\tVALUE"); } public static void dataLine(String type, String name, String data) { //Prepare for the worst mathematics in the world because I'm tired. //This could land me a job at mtgox. System.out.println(type + "\t" + tabbedData(name, 3) + data); } }
Java Code:package test; import frontend.ConsoleWriter; public class feConsoleWriter { /** * @param args */ public static void main(String[] args) { ConsoleWriter.dataHead(); ConsoleWriter.dataLine("text", "short", "some data here"); ConsoleWriter.dataLine("text", "longerversionxx", "some data here"); ConsoleWriter.dataLine("text", "evenlongerstillxxxxxxxx", "some data here"); } }
Java Code:TYPE NAME VALUE debug: tabcount=1 adding tabs... text shortsome data here debug: tabcount=2 adding tabs... text longerversionxxsome data here debug: tabcount=3 adding tabs... text evenlongerstillxxxxxxxxsome data here
Last edited by Cyrus; 04-13-2014 at 11:11 PM. Reason: forgot to paste the output
- 04-13-2014, 11:16 PM #2
Re: Formatting tables for console output
Can you post what the output is supposed to look like?
If you don't understand my response, don't ignore it, ask a question.
- 04-13-2014, 11:19 PM #3
Member
- Join Date
- Mar 2014
- Posts
- 18
- Rep Power
- 0
Re: Formatting tables for console output
It should look exactly like this...
Java Code:TYPE NAME VALUE debug: tabcount=1 adding tabs... tab text shortsome data here debug: tabcount=2 adding tabs... tab tab text longerversionxxsome data here debug: tabcount=3 adding tabs... tab tab tab text evenlongerstillxxxxxxxxsome data here
Last edited by Cyrus; 04-13-2014 at 11:22 PM.
- 04-13-2014, 11:26 PM #4
Re: Formatting tables for console output
Review how the conditional part of a for statement works.
If the condition is false, the for is NOT executed.
See line 21.If you don't understand my response, don't ignore it, ask a question.
- 04-14-2014, 12:07 AM #5
Member
- Join Date
- Mar 2014
- Posts
- 18
- Rep Power
- 0
Re: Formatting tables for console output
Just for the sake of future people Googling this, here is the finished product with remaining bugs removed.
Java Code:package frontend; public class ConsoleWriter { private static String tabbedData(String data, int min) { //System.out.println(); //for debug int tabcount=0; int len=8*min; while (data.length() < len) { tabcount++; len = len - 8; } System.out.println("debug: tabcount=" + tabcount); //System.out.print("debug adding tabs... "); for (int x=0; x < tabcount; x++) { //System.out.print("tab "); data = data + "\t"; } //System.out.println(); //for debug return data; } public static void dataHead() { System.out.println("TYPE\tNAME\t\t\tVALUE"); } public static void dataLine(String type, String name, String data) { System.out.println(type + "\t" + tabbedData(name, 3) + data); } }
Similar Threads
-
Need help formatting output
By codyjava in forum New To JavaReplies: 6Last Post: 09-21-2013, 08:34 PM -
Apply Custom Formatting to Pivot Tables, Render Chart in ODS Format
By sherazam in forum Java SoftwareReplies: 0Last Post: 05-21-2012, 11:31 AM -
Formatting Output
By Rahim2312 in forum New To JavaReplies: 18Last Post: 05-04-2012, 10:45 AM -
Help with formatting output
By John Lord in forum New To JavaReplies: 1Last Post: 10-31-2010, 01:10 PM -
Formatting output somewhat like a table
By latereg in forum New To JavaReplies: 3Last Post: 04-06-2010, 07:44 AM
Bookmarks