Results 1 to 19 of 19
Thread: Formatting Output
- 05-02-2012, 05:22 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
Formatting Output
I want to display my output in a tabular format...I need the formatting to read like this:
Name Grade Age
Richard 10 17
Sally 11 18
And so on and so forth...
I have tried using:
but that formatting doesn't exactly fit what I am wanting. Is there a way to set the tab to be a certain distance? I could probably enter two tabs between each of those, but I feel like that is just creating more coding than needed?Java Code:JOptionPane.showMessageDialog("Name" + " \t " + "Grade" + " \t " + "Age" + " \t" );
- 05-02-2012, 05:27 PM #2
Re: Formatting Output
I'm not very clear on what you're looking for. Why exactly doesn't the approach using single tabs work? You say it doesn't exactly fit what you want, but what DOES fit what you want, exactly?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-02-2012, 05:29 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
Re: Formatting Output
I just realized that for some reason the forum formatted my 1st post. Let me try it like this
Name..........................Grade............... .............Age
Well the forum keeps removing my tabs so I added "." I basically want there to be two tabs between the output./Last edited by Rahim2312; 05-02-2012 at 05:33 PM.
- 05-02-2012, 05:51 PM #4
Re: Formatting Output
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-02-2012, 05:51 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
Re: Formatting Output
How would I use two tabs?
- 05-02-2012, 05:56 PM #6
Re: Formatting Output
What have you tried? Part of coding is experimentation.
You do it the same way you use two of anything. If I want to print out one dot, I do this:
System.out.prinltn(".");
Then if I want to print out two dots, I do this:
System.out.println("..");
Same thing applies to tabs, newlines, whatever.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-02-2012, 06:00 PM #7
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
Re: Formatting Output
I tried this:
But that isn't allowing the data that pulls in below it to line up correctly based on the lenghts of eachJava Code:JOptionPane.showMessageDialog(frame, "Name" + " \t " + "\t" + "Grade" + " \t " + "\t" + "Age" );
- 05-02-2012, 06:07 PM #8
Re: Formatting Output
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-02-2012, 06:17 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Re: Formatting Output
If you really need this to be aligned then, presuming you are only going to view this in a monospaced font, you might have to look at some sort of String.format() thing.
There are ways in that of defining padding (at least there were in the old C stdlib versions on which this is based). It won't be tabs, but spaces.Please do not ask for code as refusal often offends.
- 05-02-2012, 08:35 PM #10
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
- 05-03-2012, 12:45 AM #11
Re: Formatting Output
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-03-2012, 02:54 AM #12
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
Re: Formatting Output
I tried this:
But I don't think that really fit the scenario. Is there an easy way to put Java into a "Tabular Output"Java Code:String string = "first: %s, second: %s"; String formatted = String.format(string, "one", "two"); System.out.println(formatted); // first: one, second: two
Last edited by Rahim2312; 05-03-2012 at 02:58 AM.
- 05-03-2012, 03:24 AM #13
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
- 05-03-2012, 10:03 AM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Re: Formatting Output
It would if you had read the docs on formatting (which are linked from the API for the format() method).
Give the second %s a width, which will pad spaces to the left (eg %20s).
Yes, use a JTable.Please do not ask for code as refusal often offends.
- 05-03-2012, 03:09 PM #15
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
Re: Formatting Output
Now I am on the right track...i will have to toy with this code and see if I can get this to work exactly as I need. Thank you for the help (I may be back :) )
- 05-03-2012, 04:39 PM #16
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
- 05-03-2012, 05:59 PM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Re: Formatting Output
Assuming you mean you want two variables together then pad them, you'd probably need to concatenate them first then apply them to the final format.
So something like:
Java Code:String concat = String.format("%s %s", someVar, someOtherVar); String final = String.format("%s%20s%20s", concat, third, fourth);Please do not ask for code as refusal often offends.
- 05-03-2012, 08:04 PM #18
Member
- Join Date
- Apr 2012
- Posts
- 88
- Rep Power
- 0
Re: Formatting Output
In your above code, I am assuming using concat is saying to concatenate the two variables listed above, is that correct?
I tried using that, and it produced numerous javac errors.Java Code:String concat = String.format("%s %s", input1, input2); String final = String.format("%s%20s%20s", concat, total); System.out.prtinln(final);Last edited by Rahim2312; 05-03-2012 at 08:07 PM. Reason: Forgot code tags
- 05-04-2012, 09:45 AM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Similar Threads
-
formatting data output
By droidus in forum New To JavaReplies: 16Last Post: 04-12-2011, 11:26 PM -
Help with formatting output
By John Lord in forum New To JavaReplies: 1Last Post: 10-31-2010, 12:10 PM -
formatting numbers in output
By andy3 in forum New To JavaReplies: 3Last Post: 05-27-2010, 06:43 PM -
Formatting output somewhat like a table
By latereg in forum New To JavaReplies: 3Last Post: 04-06-2010, 06:44 AM -
Need help formatting File Output
By aaroncarpet in forum New To JavaReplies: 2Last Post: 11-26-2009, 05:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks