Hi all,

I want to set an integer variable that is begins with 0 (ex: 01, 055 etc).

So I don’t want to use strings here, because I want to use this values as mathematical values..

What is the data type that i have to use here..? So please anyone can help me..

Thanks...
janaka priyadarshana

just get it as string..... and then u can convert that value in to integer
like this

String xyz="";

si=Integer.parseInt(xyz);


Its just my idea....if better solutions are there means most welcome...

You cannot store an integer value as an integer, and with a leading zero. I assume you only need the leading zero when printing the number, right?

Than look into the format method for String, if you are using at least 1.5. (And follow the "format string" link found in the method description for format.

yap.. i want it to display with leading zero..but all the numbers do not begins with zero..

so now what should i do....?

while printing only..if you want that leading zero means..i think only the way you should use string... and after that you want it as integer value means just parse it as integer...


try like this....

public class strtes
{
public static void main(String args[])
{
int x=12;
int y=10;
String a="0"+x;
String b="0"+y;
int z=Integer.parseInt(a);
int z1=Integer.parseInt(b);
System.out.println(a);
System.out.println(b);
System.out.println(z);
System.out.println(z1);
}
}

yap.. i want it to display with leading zero..but all the numbers do not begins with zero..
so now what should i do....?

I would do all my calculations and just befor displaying detect which number requeres leading zeros and how many.

I assume the number should have a specific length front padded with zeros, right? i.e.

001
002
055
100

Well, then as I said, follow the link for "format string". You call tell format that you want the number to be three digits long and front padded with zeros. Then you do not have to decide when to pad, format will.

So, I need to ask, did you actually read what I told to read, and attempt to figure it out, or do you just want "teh codes" (mispelled on purpose).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.