Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Tolls

Thread: Show zeros

  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Show zeros

    Hi

    I have a MySQL table that have a column of numbers with zeros before the number.
    All numbers have 4 digits.
    The column is of type SMALLINT(4) and ZEROFILL.

    Now I am trying to display this in a jTable in my java program.
    But how can I display the correct number format? I only gets the number after the zeros.
    I have tried int and short but no success. Any suggestions?

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

    Default Re: Show zeros

    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Show zeros

    Quote Originally Posted by Tolls View Post
    Thanks for your reply, but it don't work.

    Code:
    Java Code:
    int zipCode = 1;
    ZipCode = Integer.toString(zipCode);
    ZipCode.format("%04d", zipCode);
    Should return 0001?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Show zeros

    Yes, that method returns a formatted String but you're not using its return value; b.t.w. its a static method so there is no need to call it on an instance of the String class (ZipCode).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Show zeros

    I am not sure that I understand this completely...

    Tried this:

    Java Code:
    int zipCode = 1;
    String ZipCode = Integer.toString(zipCode);
    ZipCode.format("%04d", zipCode);
    textZipCode.setText(ZipCode);
    textZipCode is a JTextField...
    This still show 1...

  6. #6
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Show zeros

    Sorry... solved it. :)

    Java Code:
    int zipCode = 1;
    String ZipCode = String.format("%04d", zipCode);

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

    Default Re: Show zeros

    Always a good feeling to solve it yourself...:)
    And 13 minutes is pretty good going.
    asai likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Show zeros

    Yes, it feels good to solve the issues. I feel like I am almost past the beginner stage and slowly moves toward some advanced stuff soon.
    I'm getting there soon! :)

Similar Threads

  1. Why is my code just returning zeros?
    By MW130 in forum New To Java
    Replies: 8
    Last Post: 01-14-2013, 06:27 AM
  2. Replies: 3
    Last Post: 08-21-2011, 02:25 AM
  3. Trimming trailing zeros in a byte
    By Aaron_Sharp in forum New To Java
    Replies: 1
    Last Post: 01-06-2011, 11:04 AM
  4. How to display numbers with leading zeros
    By Java Tip in forum java.lang
    Replies: 1
    Last Post: 06-14-2008, 06:36 PM
  5. How to display numbers with leading zeros
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:34 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
  •