Results 1 to 15 of 15
  1. #1
    Neullson's Avatar
    Neullson is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Talking 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...

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Yup, you can use String.format(...)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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...

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Quote Originally Posted by Tolls View Post
    ETA: Bah! Serves me right for typing so much...
    But very nice with all those shiny links :)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    arun9683 is offline Member
    Join Date
    Jun 2010
    Location
    Bangalore,India
    Posts
    70
    Rep Power
    0

    Default

    If you want "078" as an integer then you can use Integer.parseInt("078");
    which will give you 78 as the output
    Arun K R,Bangalore,India
    :)

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    He's going the other direction...from 78 to "078".

  7. #7
    Neullson's Avatar
    Neullson is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    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...

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    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

  9. #9
    Neullson's Avatar
    Neullson is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    Can you make me an example code..?

  10. #10
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    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

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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.

  13. #13
    Neullson's Avatar
    Neullson is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    Thank's a lot guys. It's really helpful...

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    @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.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Blimey.
    Just gone through it myself.
    I could have sworn you could specify what character was used for the padding. Wonder what I'm thinking of then?

Similar Threads

  1. Help with checking for a certain format in a String
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 05:56 AM
  2. format content of String[]
    By eponcedeleon in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2010, 06:12 AM
  3. Format some text with Java
    By vampire in forum New To Java
    Replies: 0
    Last Post: 02-18-2010, 07:45 AM
  4. Text Format Error
    By MrFish in forum New To Java
    Replies: 2
    Last Post: 01-13-2010, 02:06 AM
  5. String.format not in javadoc
    By ortollj in forum New To Java
    Replies: 3
    Last Post: 11-08-2009, 06:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •