944,043 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 7703
  • Java RSS
Feb 26th, 2007
0

how use integer values begins with zero

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
rpjanaka is offline Offline
69 posts
since Sep 2006
Feb 26th, 2007
0

Re: how use integer values begins with zero

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...
Reputation Points: 119
Solved Threads: 7
Practically a Posting Shark
vinod_javas is offline Offline
871 posts
since Feb 2007
Feb 26th, 2007
0

Re: how use integer values begins with zero

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Feb 26th, 2007
0

Re: how use integer values begins with zero

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

so now what should i do....?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
rpjanaka is offline Offline
69 posts
since Sep 2006
Feb 26th, 2007
0

Re: how use integer values begins with zero

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);
}
}
Reputation Points: 119
Solved Threads: 7
Practically a Posting Shark
vinod_javas is offline Offline
871 posts
since Feb 2007
Feb 26th, 2007
0

Re: how use integer values begins with zero

Click to Expand / Collapse  Quote originally posted by rpjanaka ...
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.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004
Feb 26th, 2007
0

Re: how use integer values begins with zero

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).
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: I want to disable the copy option
Next Thread in Java Forum Timeline: string tokenizer





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC