473,464 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to print() colums

1 New Member
I'm grateful for any help here!

My program is generally quite a simple problem; must read in a list containing names and numbers and print it out like a phonebook in alphabetical order, in order of surname, comma, forename, and a number in a 2nd column. An example of the input is:

Fred Flintstone 555437
John Wayne 645373
Michael Schumacher 0343653
etc.

And the output should be:

Flintstone, Fred
555437
Michael, Schumacher
0343653
John, Wayne
645373

In my program I must create a class called person (strings of forename surname etc). I have an array of type Person (each array slot contains 3 strings [I use the number as a string]).

Everything works perfectly. The only problem is the printing part. As normal I'm printing the array with a for loop, concatenating the elements in the desired order, eg

static void printPerson(Person[] bk, int length)
{
for (int y = 0; y < length; y++)
{
String num = bk[y].number;
System.out.println(bk[y].surname + ", " + bk[y].forename + ('\t') + ('\t') + bk[y].number);
}
}


Of course what happens here is that when a name is above a certain length, the number will tab over even further. So it's not a solution. I was recommended to look at using printf(), but not getting the result with it. So anybody have any idea (...quite likely a simple answer!)
Mar 30 '07 #1
1 1714
horace1
1,510 Recognized Expert Top Contributor
use system.out.printf
http://java.sun.com/developer/technicalArticles/Programming/sprintf/

e.g.
Expand|Select|Wrap|Line Numbers
  1.     String s1="Fred Flintstone";
  2.     int i1=555437;
  3.     String s2="John Wayne";
  4.     int i2= 645373;
  5.     String s3="Michael Schumacher";
  6.     int i3= 0343653;
  7.     System.out.printf("%25s %10d\n", s1, i1);
  8.     System.out.printf("%25s %10d\n", s2, i2);
  9.     System.out.printf("%25s %10d\n", s3, i3);
  10.  
would print
Expand|Select|Wrap|Line Numbers
  1.           Fred Flintstone     555437
  2.                John Wayne     645373
  3.        Michael Schumacher     116651
Mar 30 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ajay Garg | last post by:
What is the way we could implement a business logic from a Table by storing it statemnnets in a colums and defining an execute sql to execute it.Some legal requirements make it diffcult for us to...
5
by: Joy | last post by:
Hi guys, I am in a tricky situation, I really really hope someone will help me. I got a layout with a main container and three colums inside. Main container: 100% height; 90% width; float: left...
0
by: Erik | last post by:
Hi, I want to make an effect like in excel when you fix colums and then you can scroll the rest colums behind the fixed. But I won't or can't do this with frames, so has anybody seen a...
0
by: Bernard Dhooghe | last post by:
In http://www-1.ibm.com/partnerworld/pwhome.nsf/weblook/eac_a_webcast_052504.html the writer says (page 50, Multidimensional Clustering Advisor): " The MDC Advisor feature of the DB2 Design...
3
by: Y. Gauthier | last post by:
Hi, I made a report with Access 2000 I show records, plus a total of some colums. I first had: totalC = col1 + col2 + col3 + col4 Then I changed it to: totalC = col1 + col2
1
by: basulasz | last post by:
May be it is a bit easy question but i don't know the way to display data in bounded colums. I get data from DB with a datareader, and I want to display them on bounded colums. I dont want to...
1
by: Eduardo78 | last post by:
I am having trouble printing in landscape, when i print directly from the grid. This is how my code looks rightnow: Private Sub cmdPrintFromGrid_Click(ByVal sender As System.Object, ByVal e...
1
by: hgm_kh | last post by:
My MS-Access application was working perfectly under WIN95/98. When I converted the MS-Access application to run on WIN-XP, some reports which have $-value colums, do no longer print as shown in...
0
by: zafar | last post by:
I don't know what property should be used for hiding colums in Data Grid, whereas in Data Grid View we have DataGridView1.Colums(index).Visible = False , But how can I hide Colums in Data Grid.....
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.