Results 1 to 6 of 6
Thread: String format output
- 09-26-2012, 07:22 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 3
- Rep Power
- 0
String format output
Hello, just wondering how I can this look nice and neat, i've tweaked around with adding tabs and the around of spaces allowed via %5d. I just can't seem to make everything line up straight, and if i do the names arn't flush with the left boundary.
here is the java code
when executed i want it to display it in a nice pretty format. Thanks for your timeJava Code://***************************************************************************** //Contacts.java //Author William Cebak //Date 25 September 2012 // // // This progam will take a text file that has a list of phone contacts. It will // take all the information on the text file and re format them and orgainze them // in a simple to read format. // //***************************************************************************** import java.util.Scanner; import java.io.*; public class Contacts { //************************************************************************* //Reads data from a file and prints their path components //************************************************************************* public static void main (String[] args) throws IOException { String Contactinfo; Scanner fileScan, textScan; int callouttotal = 0,callintotal = 0, contactcount = 0; fileScan = new Scanner (new File("C:/Users/Tony/Desktop/phoneContacts.txt")); System.out.println("\tName\t\tPhone\t\tCalls Out\tCalls In\tFavorite"); //Read and process each line of the file while(fileScan.hasNext()) { Contactinfo = fileScan.nextLine(); //reads all the //this would display the actuall text line textScan = new Scanner(Contactinfo); textScan.useDelimiter("#"); // takes out the # symbol while (textScan.hasNext()) //reads until there is no more to read { //Creats a variable for the last name and keep count for total contacts String Lastname= textScan.next(); contactcount ++; //Creates a variable for the first name String Firstname= textScan.next(); //Creates a variable for Phone Number String Phonenumber = textScan.next(); //Creates a variable for Calls out and keeps a count String Callout = textScan.next(); Integer calloutnum = Integer.valueOf(Callout); callouttotal = (callouttotal + calloutnum); //Creates a variable for calls in and keeps a count String Callin = textScan.next(); Integer callinnum = Integer.valueOf(Callin); callintotal = (callintotal+ callinnum); //Creates a variable for favorite and keeps a count String Favorite = textScan.next(); //if (Favorite == "Y") //{ // int favoritecount = 0; // favoritecount++; //} //************************************************************* // outputs the contact list in a pretty fashion :) //************************************************************* String Fullname = Firstname + " " + Lastname; System.out.printf("%s\t%12s%d\t\t%d\t\t%s",Fullname, Phonenumber, callinnum, calloutnum, Favorite); System.out.println(); } } System.out.print("\n\n"); System.out.println("Total outgoing calls for the period:\t\t" + callouttotal); System.out.println("Total incoming calls for the period:\t\t" + callintotal); System.out.println("Total contacts:\t\t\t\t\t" + contactcount); } }
- 09-26-2012, 07:33 AM #2
Member
- Join Date
- Sep 2012
- Posts
- 3
- Rep Power
- 0
Re: String format output
oh i guess it would help if you had the .txt file ...sorry
Adams#Marilyn#8233331109#0#0#N
Anderson#John#5025559980#20#15#N
Baker-Brown#Angelica#9021329944#0#3#N
Bruin#Joseph Allen#8592541109#11#5#Y
Carrolton#Louise#1079081009#4#0#N
Crisman#Francois#8591110980#11#10#Y
Davidson#George#5011231241#30#28#Y
Davis#Patrick#5021244829#5#9#N
Davis#Robert Ray#8591039492#12#0#N
Engles#Anne Marie#8591232111#17#11#Y
Engles#Kimberly#8591232110#16#10#N
Franklin#Polly#5024823482#8#8#N
Garrison#Henri#5028631678#1#10#Y
Homestead#Erica#6062332344#14#15#Y
Jackson#Camille#8595351288#57#68#Y
Kingston#Samuel#8595359990#4#19#N
Lovelace#Semone#5022709000#0#89#N
Moore#Henre#9082278023#6#9#N
Moore#Pierre#9082278888#55#30#Y
Noonan#Lyndsay#9082271110#3#33#Y
Pratt#Paula#7672778723#0#1#N
Queensly#Robert#5024499944#61#23#Y
Robertson#Victor#8595355353#0#0#N
Smith#Henri#8593380098#2#1#N
Smith#Karletta#8592230876#2#3#N
Smith#Murfey#8592230987#10#15#Y
Tucker#Amber#8593556870#2#0#N
Tucker#Victor#5027780008#0#0#N
Underwood#Fred#5025551234#0#1#Y
Voltz#Victoria#5025358630#19#0#Y
White#George#5025553412#8#2#N
White#Victoria#5024529898#2#2#N
Woods#Scott#6064843399#23#9#Y
Woodson#Bill#6065553345#18#0#N
Young#Julia#7093451129#15#11#Y
- 09-26-2012, 08:15 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: String format output
How looks like your formatted output?
- 09-26-2012, 08:32 AM #4
Member
- Join Date
- Sep 2012
- Posts
- 3
- Rep Power
- 0
- 09-26-2012, 09:43 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: String format output
%-s
The '-' means "left justified".
You might want to supply a minimum width for the names as well to ensure it all lines up, as a single tab probably isn't going to do it.
eg %-40s for a 40 character width.Please do not ask for code as refusal often offends.
- 09-26-2012, 09:44 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: String format output
Have a look at the Formatter API docs. In particular the "Flags" section talks about the - flag.
The width of items (both string and numeric) can be controlled as described in that document and the result is often more reliable than printing tab characters. The problem with tabs is that you don't always know exactly how much horizontal space they will take up.
Similar Threads
-
Formatter format method output limit?
By rykeelty in forum New To JavaReplies: 6Last Post: 04-19-2012, 06:18 PM -
How i get a UAT date format in java ? i need a output for both GMT and UAT time
By n1n in forum Advanced JavaReplies: 7Last Post: 08-11-2011, 10:06 PM -
output format problem.
By jim01 in forum New To JavaReplies: 8Last Post: 04-18-2011, 09:00 AM -
Format the output
By Moustafa taha in forum New To JavaReplies: 5Last Post: 10-18-2010, 05:01 AM -
output for list of names in a format
By Ms.Ranjan in forum New To JavaReplies: 7Last Post: 06-18-2009, 04:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks