Results 1 to 8 of 8
  1. #1
    009
    009 is offline Member
    Join Date
    May 2014
    Posts
    22
    Rep Power
    0

    Default triming leading zeros

    hi i got situation where i have postal code as 0009 in database and the use is entering 0009 but somehow in my java code it only reading 9 from the xml file

    this is how i define getter and setter

    when i deburg the code i get
    passed postalcode 9

    Java Code:
        
    public String Postalcode="";
    
    
        public void setPostalcode(String Postalcode) {
            this.Postalcode = Postalcode;
        }
    
        public String getPostalcode() {
            return Postalcode;
        }
    i what if 0009 is passed i what it to remain 0009
    Last edited by 009; 06-10-2014 at 12:11 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: triming leading zeros

    Obviously the problem is not in the setter which does absolutely -nothing-, its rather in the logic which reads the data from the file. But since you provide absolutely no information, its impossible to help you.

    Besides that, how did you check the value to know it is wrong?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: triming leading zeros

    Show the code where you read the post code that is then going wrong.

    Somewhere you are probably reading it as an integer.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: triming leading zeros

    Duplicate thread:

    https://community.oracle.com/thread/3570535

    If you post your question to other forums, be sure to post a link to it so others can follow it and see what has already been answered. That prevents them from having to do unnecessary effort.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    009
    009 is offline Member
    Join Date
    May 2014
    Posts
    22
    Rep Power
    0

    Default Re: triming leading zeros

    i check in database is 0009 and in xml file the user enter 0009

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

    Default Re: triming leading zeros

    And?
    You have my response here, and two over at OTN, all asking you to show HOW you are reading this data and from WHERE, because it is almost certain you are reading it as an integer somewhere along the line.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    pbj
    pbj is offline Member
    Join Date
    Jun 2014
    Posts
    15
    Rep Power
    0

    Default Re: triming leading zeros

    009,
    If you simply want to output a value with leading zeros (for a total of 4 columns), you'll have to specify that in your overridden toString() method in your class, with the String.format() and appropriate flags. I have a small sample here, with its output and hope that it will help you.
    Java Code:
    package sample;
    
    class TestC{
    	private int test;
    	
    	public TestC() {
    		test = 0;
    	}
    	
    	public TestC(int t) {
    		test = t;
    	}
    	
    	public String toString(){
    		return String.format("%04d", test);
    	}
    }
    class LeadingZeros {
    	public static void main(String[] args) {
    		TestC tc1 = new TestC(9);
    		TestC tc2 = new TestC(19);
    		TestC tc3 = new TestC();
    
    		System.out.println(tc1.toString());
    		System.out.println(tc2.toString());
    		System.out.println(tc3.toString());
    	}
    }
    outputs:
    0009
    0019
    0000
    Beyond that, I am curious as to what country's actually use 4 digit postal codes...

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

    Default Re: triming leading zeros

    Quote Originally Posted by pbj View Post
    Beyond that, I am curious as to what country's actually use 4 digit postal codes...
    A very small country?
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

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