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?

The Java Tutorials - String

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

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

you can write

String fs;
    fs = String.format("The value of the float variable is %f, while the value of the " + 
                       "integer variable is %d, and the string is %s", floatVar, intVar, stringVar);
    System.out.println(fs);

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

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.

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.

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

alisn you should start up your own thread rather than reviving old threads.

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

Chris

alisn you should start up your own thread rather than reviving old threads.

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

output+= String.format("%1$10d%2$10.0f%3$10d%4$10.0f\n", i, QQ, d, inventory);
commented: Love your work.i figured it out by studying what you gave me..thanx heaps +0

thanx heaps man i studied your code and after some modifications got it to work for me....really appreciate it....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.