-
String formatter
Hi! Is there a function in java that is similar to C's string formatter?
For example, I want that the minimum length of a inetger (string) be 3 characters with zeros padded to the left:
for(int i=0 i< 1000; i++){
String s = new String("" + i);
//some string formatting code;
System.out.println(s)
}
I need the output to be like this:
000
001
002
003
....
999
Is there anyway to do this in java?
thanks
-
maybe you can use the int...
Code:
String s = new String("");
for(int i=0 i< 1000; i++)
{
if ( i < 10 )
{
s = "00" + i;
}
else if ( i < 100 )
{
s = "0" + i;
}
else
s = "" + i;
}
I think that this the easiest way is....
grz,
Kootsj
-
I wouldn't do all of that just use the formatting capabilities java already gives you here is a demonstration what you want to do is use printf like so
public class printingnumbers
{
{
for(int i=0; i< 1000; i++){
System.out.printf("%03d\n", i);
}
}
}
you start with double quotes then a percent sign then a 0 for leading zeros then how many places you want which is 3 to the left of the decimal then d which I think stands for decimal maybe not but it's what you use for integers.
Here's the output you would get up to 100
000
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
study up on what java already provides you it will make your programming life easier.
-
Thanks, Is there a way I could store it to string? since I would be writing it to a file...
-
Here's a sample program from java how to program sixth edition that writes text to a file anything you can do with printf you can do with Formatter object
// Fig. 14.7: CreateTextFile.java
// Writing data to a text file with class Formatter.
import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import com.deitel.jhtp6.ch14.AccountRecord;
public class CreateTextFile
{
private Formatter output; // object used to output text to file
// enable user to open file
public void openFile()
{
try
{
output = new Formatter( "clients.txt" ); } // end try
catch ( SecurityException securityException )
{
System.err.println(
"You do not have write access to this file." );
System.exit( 1 );
} // end catch
catch ( FileNotFoundException filesNotFoundException )
{
System.err.println( "Error creating file." );
System.exit( 1 );
} // end catch
} // end method openFile
// add records to file
public void addRecords()
{
// object to be written to file
AccountRecord record = new AccountRecord();
Scanner input = new Scanner( System.in );
System.out.printf( "%s\n%s\n%s\n%s\n\n",
"To terminate input, type the end-of-file indicator ",
"when you are prompted to enter input.",
"On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
"On Windows type <ctrl> z then press Enter" );
System.out.printf( "%s\n%s",
"Enter account number (> 0), first name, last name and balance.",
"? " );
while ( input.hasNext() ) // loop until end-of-file indicator
{
try // output values to file
{
// retrieve data to be output
record.setAccount( input.nextInt() ); // read account number
record.setFirstName( input.next() ); // read first name
record.setLastName( input.next() ); // read last name
record.setBalance( input.nextDouble() ); // read balance
if ( record.getAccount() > 0 )
{
// write new record
output.format( "%d %s %s %.2f\n", record.getAccount(),
record.getFirstName(), record.getLastName(),
record.getBalance() ); } // end if
else
{
System.out.println(
"Account number must be greater than 0." );
} // end else
} // end try
catch ( FormatterClosedException formatterClosedException )
{
System.err.println( "Error writing to file." );
return;
} // end catch
catch ( NoSuchElementException elementException )
{
System.err.println( "Invalid input. Please try again." );
input.nextLine(); // discard input so user can try again
} // end catch
System.out.printf( "%s %s\n%s", "Enter account number (>0),",
"first name, last name and balance.", "? " );
} // end while
} // end method addRecords
// close file
public void closeFile()
{
if ( output != null )
output.close();
} // end method closeFile
} // end class CreateTextFile
/*************************************************************************
* (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
Just use the same leading zero technique I showed you above it should work exactly the same like output.format( "%03d %s %s %.2f\n", record.getAccount(),
I highlighted some of the important points here but not all of them look at this if you still have a question just ask.
Similar Threads
-
By mdengler in forum ASP.NET
Replies: 0
Last Post: 11-26-2002, 02:32 PM
-
By Fred Mayes in forum Java
Replies: 1
Last Post: 06-05-2001, 06:12 AM
-
By Julian Milano in forum VB Classic
Replies: 2
Last Post: 08-11-2000, 12:11 PM
-
By chandra in forum VB Classic
Replies: 0
Last Post: 06-22-2000, 07:36 AM
-
By Robert Rieth in forum VB Classic
Replies: 1
Last Post: 04-11-2000, 03:21 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks