Results 1 to 15 of 15
Thread: Text/String format..??
- 07-22-2010, 11:02 AM #1
Text/String format..??
Hello guys, I'm new in this forum and JAVA...
OK, here is the case. I want to build a Library Information System. I need to add BookSerialNumber (BSN). This BSN format goes like this : YYYY-CC-DDD. I'll explain it, YYYY = Year of the book published, CC = Category of the book, DDD = Digit count of the book according to Year and Category. Example : 2010-14-078. It's like lpad() function in SQL.
Example :(When I input...)
a) 78, the output 078
b) 4, the output 004
Is there any class or method or algoritm that I can use to make this text/String format?
Ok, that's my case. And I'm sorry if my English isn't so good. Thank's...
- 07-22-2010, 11:27 AM #2
Yup, you can use String.format(...)
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-22-2010, 11:30 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
There's a format() method on String.
It takes the same format String as used by java.util.Formatter.
So reate a format string that matches the output you want (three parameters, one for year, one for category, one for count) and pass that and the various parameters into the format() method.
ETA: Bah! Serves me right for typing so much...
- 07-22-2010, 11:36 AM #4
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-22-2010, 12:21 PM #5
Member
- Join Date
- Jun 2010
- Location
- Bangalore,India
- Posts
- 70
- Rep Power
- 0
If you want "078" as an integer then you can use Integer.parseInt("078");
which will give you 78 as the outputArun K R,Bangalore,India
:)
- 07-22-2010, 12:22 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
He's going the other direction...from 78 to "078".
- 07-22-2010, 03:58 PM #7
Hmm.. I'm sorry, I'm new to Java. Can you guys give a simple example code, please..?? Because I don't know how to use it...
It's like we have background string of 7 character '*******', so when I Input 'Word', the result is '***Word'. Or if I want the background string is '0000', so when I input '4', the result is '0004'.
Thank you...
- 07-22-2010, 04:03 PM #8
Did you follow the links that Tolls has posted? What didn't you understand?
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-22-2010, 04:33 PM #9
Can you make me an example code..?
- 07-22-2010, 04:38 PM #10
Yes:
Java Code:System.out.println(String.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d"));Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-22-2010, 04:42 PM #11
If your formatting is as simple as you show, you could use String concatenation and substring to get the output:
String output = ("00000" + "4").substring(2); ==> 0004
String output = ("00000" + "54").substring(3); ==> 0054
A bit of math will give you the substring arg to use based on leading chars, length of given string and length of desired results.
- 07-22-2010, 04:48 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
The formatter stuff, though, is handy to know as it's a standard (near as). I'd really recommend attempting it. It's all explained in those two links.
- 07-23-2010, 02:52 AM #13
Thank's a lot guys. It's really helpful...
- 07-23-2010, 03:02 AM #14
@Tolls I scanned the formatter doc and don't see how to format: ***WORD
A n(here 7) space result with a String right adjusted in it and *s(or any char) as filler.
I was looking for filler or default but didn't see it.
- 07-23-2010, 08:51 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Similar Threads
-
Help with checking for a certain format in a String
By SteroidalPsycho in forum New To JavaReplies: 2Last Post: 03-26-2010, 04:56 AM -
format content of String[]
By eponcedeleon in forum Advanced JavaReplies: 9Last Post: 02-23-2010, 05:12 AM -
Format some text with Java
By vampire in forum New To JavaReplies: 0Last Post: 02-18-2010, 06:45 AM -
Text Format Error
By MrFish in forum New To JavaReplies: 2Last Post: 01-13-2010, 01:06 AM -
String.format not in javadoc
By ortollj in forum New To JavaReplies: 3Last Post: 11-08-2009, 05:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks