1,105,585 Community Members

how use integer values begins with zero

Member Avatar
rpjanaka
Junior Poster in Training
69 posts since Sep 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
vinod_javas
Practically a Posting Shark
871 posts since Feb 2007
Reputation Points: 65 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

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...

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

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.

Member Avatar
rpjanaka
Junior Poster in Training
69 posts since Sep 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

so now what should i do....?

Member Avatar
vinod_javas
Practically a Posting Shark
871 posts since Feb 2007
Reputation Points: 65 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

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);
}
}

Member Avatar
peter_budo
Code tags enforcer
7,935 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
0
 

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.

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

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).

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article