944,159 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 15162
  • Java RSS
Nov 5th, 2007
0

How do you output in two columns?

Expand Post »
Annual Interest Rate Monthly Payment

11.00 952.32
11.25 971.26
11.50 990.29
11.75 1009.41
12.00 1028.61



I've done the hard part and completed the project, but I can't figure out how to get my program to output in two columns like above. Since this site doesn't take spacing, black column is one column and the red is the other column. Do you just wing it and add a bunch of spaces in or is there a proper way to do it?
Last edited by Amurka; Nov 5th, 2007 at 6:04 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Amurka is offline Offline
8 posts
since Oct 2007
Nov 5th, 2007
0

Re: How do you output in two columns?

The Java Tutorials - String
Quote ...
Using String's static format() method allows you to create a formatted string that you can reuse, as opposed to a one-time print statement. For example, instead of

Java Syntax (Toggle Plain Text)
  1. System.out.printf("The value of the float variable is %f, while the value of the " +
  2. "integer variable is %d, and the string is %s", floatVar, intVar, stringVar);

you can write

Java Syntax (Toggle Plain Text)
  1. String fs;
  2. fs = String.format("The value of the float variable is %f, while the value of the " +
  3. "integer variable is %d, and the string is %s", floatVar, intVar, stringVar);
  4. System.out.println(fs);
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Nov 6th, 2007
0

Re: How do you output in two columns?

That method is pretty similar to the printf() method of String. What you could also do is, if you only have a couple of things, you could tab them, ie:
System.out.println( "Blah\tBlah" ); would give you an output like:

Blah Blah
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
mickinator is offline Offline
55 posts
since Oct 2007
Nov 6th, 2007
0

Re: How do you output in two columns?

As long as the first String is always of the same length and does not vary over the "tabStop index" length. printf (if you're using a late enough version of it) is, of course, the proper way to do this.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 6th, 2007
0

Re: How do you output in two columns?

This is a lot easier than when I first looked at it. I made the first column stuff string and took there length, then from that added the necessary spaces. Kind of easy. Thanks for the help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Amurka is offline Offline
8 posts
since Oct 2007
Apr 8th, 2010
0
Re: How do you output in two columns?
ok i've read this thread 4 times now and still cant get mine to work...i've tried tabbing, and format specifiers with String.format but no luck..anything that has worked for me so far is literally putting spaces as string...

this is my line of code:

output = output+ String.format(" "+i+" "+OQ+" "+d+" "+inventory+"\n");

declarations are:

int i, d;
double OQ,inventory;

i need them all to have even spaces between them and line up perfectly in 4 columns when the program loops the calculations...

i changes (its the counter of the weeks)

thanx guys
i need some help by sunday..cheers
im a total newby...
Last edited by alisneaky; Apr 8th, 2010 at 8:05 am. Reason: missing code
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alisneaky is offline Offline
3 posts
since Apr 2010
Apr 8th, 2010
0
Re: How do you output in two columns?
alisn you should start up your own thread rather than reviving old threads.

java Syntax (Toggle Plain Text)
  1. System.out.printf("%1$10d%2$11.2f%3$10d%4$11.2f\n", i, QQ, d, inventory);

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Apr 8th, 2010
0
Re: How do you output in two columns?
alisn you should start up your own thread rather than reviving old threads.

java Syntax (Toggle Plain Text)
  1. System.out.printf("%1$10d%2$11.2f%3$10d%4$11.2f\n", i, QQ, d, inventory);

Chris
ok well i tried and it didnt work...i dunno if i've explained it right or not..

basically output has gotta look like this.

Week Quantity Demand Inventory
Order
1 87 45 42
2 87 45 84
3 0 45 39
4 87 45 81
5 0 45 36
6 9 45 0

Now: Because Quantity Order and Inventory are doubles in declaration for calculation purposes, i need them to be shown with no decimal places.

also since i'm using GUI...this is my complete line that has to get modified...

output = output + String.format("<specifies>", i, OQ, d, inventory);

i just dunno what specifiers and whether String.format is right or not...i know GUI doesnt use System.out.printf...

thanx again chris for the effort...my whole code is around 260 lines and this is the only line that's really giving me the shits...

if you can help me you'll be my hero..
thanx heaps..
Last edited by alisneaky; Apr 8th, 2010 at 11:37 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alisneaky is offline Offline
3 posts
since Apr 2010
Apr 8th, 2010
1
Re: How do you output in two columns?
java Syntax (Toggle Plain Text)
  1. output+= String.format("%1$10d%2$10.0f%3$10d%4$10.0f\n", i, QQ, d, inventory);
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Apr 8th, 2010
0
Re: How do you output in two columns?
thanx heaps man i studied your code and after some modifications got it to work for me....really appreciate it....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alisneaky is offline Offline
3 posts
since Apr 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Edit Title of Save Dialog Box
Next Thread in Java Forum Timeline: Putting pictures into Swing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC