how use integer values begins with zero

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2006
Posts: 68
Reputation: rpjanaka is an unknown quantity at this point 
Solved Threads: 0
rpjanaka rpjanaka is offline Offline
Junior Poster in Training

how use integer values begins with zero

 
0
  #1
Feb 26th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 871
Reputation: vinod_javas is an unknown quantity at this point 
Solved Threads: 7
vinod_javas's Avatar
vinod_javas vinod_javas is offline Offline
Practically a Posting Shark

Re: how use integer values begins with zero

 
0
  #2
Feb 26th, 2007
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...
Adios,

Vinod......
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: how use integer values begins with zero

 
0
  #3
Feb 26th, 2007
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 68
Reputation: rpjanaka is an unknown quantity at this point 
Solved Threads: 0
rpjanaka rpjanaka is offline Offline
Junior Poster in Training

Re: how use integer values begins with zero

 
0
  #4
Feb 26th, 2007
yap.. i want it to display with leading zero..but all the numbers do not begins with zero..

so now what should i do....?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 871
Reputation: vinod_javas is an unknown quantity at this point 
Solved Threads: 7
vinod_javas's Avatar
vinod_javas vinod_javas is offline Offline
Practically a Posting Shark

Re: how use integer values begins with zero

 
0
  #5
Feb 26th, 2007
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);
}
}
Adios,

Vinod......
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,267
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 493
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: how use integer values begins with zero

 
0
  #6
Feb 26th, 2007
Originally Posted by rpjanaka View Post
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.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: how use integer values begins with zero

 
0
  #7
Feb 26th, 2007
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).
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 4328 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC