Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Printing Question 1

Status
Not open for further replies.

thumpkidd9

Programmer
Mar 27, 2005
42
0
0
US
Hello,
I have always wondered this but finally had to implement something that needed it. I want to print values to the screen but with some kind of order. I'm using println() with '\t' character but its not lining up at all. I know there must be a better way to line up the data, which can be entered dynamically so I dont know the size of the space needed. I'm looking for something like this. Nothing too in depth but clean.

No. Price Paid

1. 20.00 30.00

Thanks,

 
The best way I've ever come up with (and I'm a real novice so there are bound to be better ways) is to use a Char array to place the individual items within, and then print the Char array.

So, I declare the array:
char[] ttl;

then I set its length (based on how may characters wide the whole display is to be):
ttl = new char[stln];

then, each time after I've printed it, I need to zero it out:
for (int i=0;i<stln;i++) {ttl=" ".charAt(0);}

then I put characters in the array from strings I want to display. For example, a string, title:
for (int j=0; j<title.length(); j++) {ttl[j+((i+1)*colwdth)]=title.charAt(j);}

In my case, I'm displaying the data in a TextArea, ta1:
ta1.append(new String(ttl)+"\n");

_________________
Bob Rashkin
 
To answere sedj, I am printing strings to the screen, for example this is way off and depends on the length of dynamically entered values for price, paid..etc:

Code:
System.out.println("No. \tPrice \tPaid \tAddress \tDevelopment \tCash/Credit");

System.out.println((x+1)+". "+curr.price+"\t"+curr.paid+"\t"+curr.addr+"\t\t"+curr.dev+"\t\t"+curr.cacr);

Bong, you had me till TextArea. Is that only for applets? Because I've never used them and wasn't planning on messing with them till later.

Thanks for help
 
No, a TextArea is just a GUI device for writing text. Just println() the string constructed from the character array.

_________________
Bob Rashkin
 
Great ! I dont have time at the moment, but I will give that a shot in the afternoon and keep you posted. Thanks for fast response.
 
create a SpecialString which contains a string.

SpecialString(string, width); //constructor for string and width to print it

SpecialString.toPrint() should be overridden to print based on the width passed in.
 
h0h0h0: I'm not sure how SpecialString works and don't know what I need to import to compile that, my java isn't picking it up.

Bong: I tried your method but I am a little confused. Were the for loops supposed to be nested because you are referencing i in the second one. Also you said, "then, each time after I've printed it" Did you mean printed the char array?

Sorry for the confusion,
Matt
 
Sorry. I was just putting in what I thought were the highlights. Here's some more of the details.

I set a column width:
int colwdth=11;

I have a number of columns (in this case I call it clen. I define the string length in terms of the number of columns and the column width:
stln=(clen+1)*colwdth;
ttl = new char[stln];


Then I calculate what it is I want to display in an array. I loop through the columns, inserting the characters in the character array:
for (int i=0;i<clen;i++) {
title = String.valueOf(totals[indx]); [red]//this is the string value to be displayed[/red]
for (int j=0;j<title.length();j++) {
ttl[((i+1)*colwdth)+j]=title.charAt(j);[red]//j indexes each character in the value I want to insert[/red]
}
}


then I display the string made from the character array:
ta1.append(new String(ttl)+"\n");

You would probably use
println(new String(ttl));


_________________
Bob Rashkin
 
Try this:
Code:
class colprn {
//    main method
    public static void main(String args[]) {
         int numcols = 4;
         int colwdth = 5;
         char holder[] = new char[numcols*colwdth];
         String s[]=new String[4];
         String sval;
         s[0]="abc";
         s[1]="def";
         s[2]="ghi";
         s[3]="jkl";
         for (int rown=0;rown<4; rown++) {
               for (int i=0; i<numcols*colwdth; i++) {holder[i]=" ".charAt(0);}
               for (int i=0;i<numcols;i++) {
                   sval = s[i];
                   for (int j=0;j<sval.length();j++) {
                       holder[(i*colwdth)+j]=sval.charAt(j);
                   }
               }
               System.out.println(new String(holder));
         }

    }
}

_________________
Bob Rashkin
 
Thanks for the time and effort you spent on helping me. I got it to work and its working good. All I modified is if a string was entered that was larger than my colwdth, I made a substring of it and added ".." to the end. The next part that I am going to try and do is instead of one colwdth value, have a dynamic value depending on what column your in. This way, for example the two fields, Address and Male/Female can get an appropiate alloted space, say 25 for Address and 2 for Male/Female.

Thanks again,
~matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top