Results 1 to 5 of 5
  1. #1
    Cyrus is offline Member
    Join Date
    Mar 2014
    Posts
    18
    Rep Power
    0

    Default 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);
    	}
    }
    The main method is in a testing class intended to test this class.

    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");
    	}
    
    }
    The output...
    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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,790
    Rep Power
    25

    Default 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.

  3. #3
    Cyrus is offline Member
    Join Date
    Mar 2014
    Posts
    18
    Rep Power
    0

    Default 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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,790
    Rep Power
    25

    Default 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.

  5. #5
    Cyrus is offline Member
    Join Date
    Mar 2014
    Posts
    18
    Rep Power
    0

    Default 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);
    	}
    }
    And, thanks!

Similar Threads

  1. Need help formatting output
    By codyjava in forum New To Java
    Replies: 6
    Last Post: 09-21-2013, 08:34 PM
  2. Replies: 0
    Last Post: 05-21-2012, 11:31 AM
  3. Formatting Output
    By Rahim2312 in forum New To Java
    Replies: 18
    Last Post: 05-04-2012, 10:45 AM
  4. Help with formatting output
    By John Lord in forum New To Java
    Replies: 1
    Last Post: 10-31-2010, 01:10 PM
  5. Formatting output somewhat like a table
    By latereg in forum New To Java
    Replies: 3
    Last Post: 04-06-2010, 07:44 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •