How can I have my Java Output be in a Tabular Formating?
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.
How can I have my Java Output be in a Tabular Formating?
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.
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 08:55 PM.
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.
Sorry - lame attempt at a joke. Look at the second and third links. They have example code to help you display things in columns.
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!
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.
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 10:05 PM.
printf() and StringBuilder are both useful for assembling and formatting textual output.
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
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"
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.
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)
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.
Okay you lost me on your post, I don't understand what you mean?
The runtime is complaining about the stray space you have on line 43. (after the third percentage sign)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)