Results 1 to 4 of 4
  1. #1
    AndyC is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default short month = Short.parseShort(splitarray[2].toString()));

    Hello,

    I am parsing a string and need to store the value as a short.

    Using the following line of code:

    short month = Short.parseShort(splitarray[2].toString()));

    Although its returning 2 instead of 02.

    How to preserve the actual value of 02?

    I've reversed engineered a schema, that is not controlled by me. As a result the value will need to be stored as a short instead of string.


    Thanks, AC.

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Well, since 2 == 02 == 002 == 0002 I don't see where the problem is? If you just want the 02 at output you could simply do this:
    Java Code:
    Short s = someMethod(); //notice the upper case
    String a = s.toString();
    if(a.length() == 1)
    a = "0"+a;
    This will append the 0 to months 1-9 and keep months 10-12 as they are.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    AndyC is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Yeah I could do that if I could store the value as a string.

    The schema requires a short value. I'm generating an XML file using CastorXML and have reversed engineered the objects based on a schema that I have no control over.

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

    Default

    Quote Originally Posted by AndyC View Post
    Yeah I could do that if I could store the value as a string.

    The schema requires a short value. I'm generating an XML file using CastorXML and have reversed engineered the objects based on a schema that I have no control over.
    As m00nchile already wrote: ints don't have leading zeros. If you want to display one with those leading zeros you have to convert it to a String and prepend those leading zeros; M00nchile already gave you a solution. You don't have to store anything as a String type, you just have to convert your short to a String, fiddle a bit with it and display it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. two short codes
    By Libertyman in forum New To Java
    Replies: 7
    Last Post: 06-21-2010, 03:22 PM
  2. Need help with short problem
    By fataguila in forum New To Java
    Replies: 1
    Last Post: 01-30-2010, 08:58 AM
  3. Short Circuiting?
    By Philly0494 in forum New To Java
    Replies: 12
    Last Post: 11-12-2009, 10:45 AM
  4. Short-cut key
    By tskumarme in forum New To Java
    Replies: 2
    Last Post: 05-28-2008, 04:16 PM
  5. Short/Integer
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-06-2007, 09:28 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
  •