Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Having Java output in Tabular Format

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Having Java output in Tabular Format

    How can I have my Java Output be in a Tabular Formating?


  2. #2
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: Having Java output in Tabular Format

    Can you describe a bit more about what you're trying to do? Are you outputing to a command line terminal? A web page? A papyrus? Help us help you.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Having Java output in Tabular Format

    I want to display 2 Columns of output using JOptionPane
    Formula............Answer
    1+1.................2
    1-1.................0

    The Formula is pulled from user input so it will be different, but I think that will give a better idea of what I am trying to accomplish.

    EDIT---
    Of course I want it to display without all of the "." showing but I had to add those for formatting to display properly
    Last edited by jo15765; May 2nd, 2012 at 09:55 PM.

  4. #4
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: Having Java output in Tabular Format

    I had no idea myself. So I went to the Javadoc to learn more. That redirected me to the dialog tutorial. Running that showed a nice example that had things in columns and included some source code.

    Use the Javadoc Luke.

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Having Java output in Tabular Format

    Quote Originally Posted by stdunbar View Post
    I had no idea myself. So I went to the Javadoc to learn more. That redirected me to the dialog tutorial. Running that showed a nice example that had things in columns and included some source code.

    Use the Javadoc Luke.
    I don't see the Javadoc Luke you are referencing. I am browsing the site now, and am lost

  6. #6
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: Having Java output in Tabular Format

    Sorry - lame attempt at a joke. Look at the second and third links. They have example code to help you display things in columns.

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Having Java output in Tabular Format

    I am looking at the 2nd link, and haven't seen anything yet, but I did just notice that there was a "Next" option at the bottom of the page, so I am scrolling throu the pages searching for it!

  8. #8
    Junior Member
    Join Date
    May 2012
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Having Java output in Tabular Format

    Quote Originally Posted by jo15765 View Post
    I am looking at the 2nd link, and haven't seen anything yet, but I did just notice that there was a "Next" option at the bottom of the page, so I am scrolling throu the pages searching for it!
    Okay still not seeing anything? Am I just blind?

  9. #9
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: Having Java output in Tabular Format

    Did you run the demo? If so, did you see that on the "Dialog Icons" tab there is tabular data? With that, did you look at the source code? And did you happen to see the method create2ColPane?

    I doubt you're blind You're in a hurry for the simple answer. Ultimately you need a GridLayout.

  10. #10
    Junior Member
    Join Date
    May 2012
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Having Java output in Tabular Format

    I see it now, thank you! I honestly wasn't 100% sure what I was searching for. This is a pretty advanced coding IMO, WAY WAY more detailed than what I was thinking it was going to take to complete this task.

    EDIT ---
    This is showing for creating buttons on a GUI. I just want to display text, is it the same principal?
    Last edited by jo15765; May 2nd, 2012 at 11:05 PM.

  11. #11
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    974
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Having Java output in Tabular Format

    printf() and StringBuilder are both useful for assembling and formatting textual output.

  12. #12
    Junior Member
    Join Date
    May 2012
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Having Java output in Tabular Format

    I googled StringBuilder earlier, but the examples I saw didn't really fit my specifications (or at least to my understanding didnt). I will google printf() now and see if I can find something like that. Thank you for the response

  13. #13
    Junior Member
    Join Date
    May 2012
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Having Java output in Tabular Format

    So if I am understanding printf() properly, I set the length for my variables, correct? So would this syntax work:
       StringBuilder sb = new StringBuilder();
       // Send all output to the Appendable object sb
       Formatter formatter = new Formatter(sb, Locale.US);
     
       // Explicit argument indices may be used to re-order output.
       formatter.format("%1$0s %1$0s %1$0s %1$0s", "customerInput1", "customerInput2", "answer1", "answer2")
       // -> " answer2  answer1 customerInput2  customerInput1"

  14. #14
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    974
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Having Java output in Tabular Format

    I don't think you need those $ things in the format string unless you do want to have the output in a different order from that of the arguments (or want to reuse arguments). If you do want that then there has to be different number in front of the $ as in the example on the Formatter API page.

    The columns are controlled by the numbers in front of the s element: these determine the width of the string and columns emerge if the same format is used on each line. In code it looks like:

     
    public class PrintfEg {
     
        public static void main(String[] args) {
            String fmt = "%-7s %-12s %s%n";
     
            System.out.printf(fmt, "First", "Some text", "goes here");
            System.out.printf(fmt, "Second", "Short", "strings");
            System.out.printf(fmt, "Third", "Longer text", "goes here");
            System.out.printf(fmt, "Last", "Some text", "goes here");
        }
    }

    Notice how negative numbers are used to indicate left aligning.

    The StringBuilder comes into play if you want to accumulate all the strings into one long one. There is an example of how to use StringBuilder on the Formatter API page as well.

  15. #15
    Junior Member
    Join Date
    May 2012
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Having Java output in Tabular Format

    I tried to tweak that code slightly and it didn't work
            String fmt = "%-7s %-7s % -7s %s%n";
     
            System.out.printf(fmt, userinput1, "+", userinput2, total);

    That's the code I was just trying to test....everything except the Plus sign is a variable. Userinput1 and 2 are of course input by the user and in int formatting. Total as you can see is the sum of UI1 + UI2 and is also in int format. Using javac it compiled fine, but when I actually try to run the program I get this:

    Exception in thread "main" java.util.FormatFlagsConversionMismatchException: Con
    version = s, Flags =
    at java.util.Formatter$FormatSpecifier.failMismatch(U nknown Source)
    at java.util.Formatter$FormatSpecifier.checkBadFlags( Unknown Source)
    at java.util.Formatter$FormatSpecifier.checkGeneral(U nknown Source)
    at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
    at java.util.Formatter.parse(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.io.PrintStream.format(Unknown Source)
    at java.io.PrintStream.printf(Unknown Source)
    at CodeTest.main(Code.java:43)

  16. #16
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: Having Java output in Tabular Format

    Look at your post describing what you wanted to do. Is it misaligned on your screen? If it is it is because it isn't a fixed width font.

    System.out is most likely going to use fixed width fonts on your system. Now you have to use fixed fonts in your UI so that the space characters take up the same amount of space as the "real" characters.

  17. #17
    Junior Member
    Join Date
    May 2012
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Having Java output in Tabular Format

    Okay you lost me on your post, I don't understand what you mean?

  18. #18
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    974
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Having Java output in Tabular Format

    Exception in thread "main" java.util.FormatFlagsConversionMismatchException: Con
    version = s, Flags =
    at java.util.Formatter$FormatSpecifier.failMismatch(U nknown Source)
    at java.util.Formatter$FormatSpecifier.checkBadFlags( Unknown Source)
    at java.util.Formatter$FormatSpecifier.checkGeneral(U nknown Source)
    at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
    at java.util.Formatter.parse(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.io.PrintStream.format(Unknown Source)
    at java.io.PrintStream.printf(Unknown Source)
    at CodeTest.main(Code.java:43)
    The runtime is complaining about the stray space you have on line 43. (after the third percentage sign)

Similar Threads

  1. Issue with output format
    By mael331 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 25th, 2011, 03:12 PM
  2. Format difference in System.out Vs. file output
    By mwr76 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 10th, 2011, 10:13 PM
  3. Format Java output
    By wildman0501 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 11th, 2011, 07:09 PM
  4. J2ME-Show RMS data in tabular form
    By Sunil Raghuvanshi in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: January 24th, 2011, 08:06 AM
  5. Format some text with Java
    By vampire in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 18th, 2010, 11:30 AM