Results 1 to 2 of 2
  1. #1
    paul is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

    Default it's possible format data in java?

    In java, I know it's possible to format words or numbers. An example would be
    Java Code:
    System.out.printf("%15s\n", word);
    Which would format whatever the value of word in a space with size 15.

    Somewhere I read it's possible to do the same if you are returning a value, but did not provide any examples of it. I tried:

    Java Code:
    return %15s\n,word;
    ovbiusly I'm trying to return "word", can I format it while I return it?

    That doesn't work, as I get illegal start of expression as an error.
    Thanks

  2. #2
    coco is offline Member
    Join Date
    Jul 2007
    Posts
    39
    Rep Power
    0

    Default

    Not exactly. It doesn't "return" it at all, it just sends it to the out stream using printf...

    If you really needed to format a string you could do so through the string
    utilities. For example, substring should be able to cut off the part of the string you want.

    String (Java 2 Platform SE v1.4.2)

    This means however that you will have to check to see if the string is too short. If you don't you'll get an outofbounds exception. So just check the length, if it's less then what you want to crop it to, just return it, else, crop it with substring and return it.
    Greetings.

Similar Threads

  1. DateField in format : HH:mm:ss:ms
    By gibsonsydsa in forum CLDC and MIDP
    Replies: 4
    Last Post: 03-10-2008, 07:46 AM
  2. Timestamp in SQL format
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-10-2008, 11:37 AM
  3. Replies: 0
    Last Post: 11-06-2007, 02:20 PM
  4. Code format
    By Eric in forum XML
    Replies: 1
    Last Post: 07-06-2007, 07:44 AM
  5. Code format
    By Eric in forum XML
    Replies: 1
    Last Post: 07-06-2007, 07:42 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
  •