943,850 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 7824
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 11th, 2009
0

how to compare a string to an integer in java

Expand Post »
hiiii frns...


integer can hold only upto 8 bits...where as a string can hold more than 8 bits...so to compare them...is der any function???

like my code is like this

for(int i=0;i<= stringgg("12345678901234567890"); i++)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
koolhoney07 is offline Offline
7 posts
since Mar 2009
Mar 11th, 2009
0

Re: how to compare a string to an integer in java

1. Java integers are 32 bits.
2. Java Strings are 16 bits per character

3. To compare them or use the String in arithmetic expressions, you first need to convert the string to integer with something like int i = new Integer("12345");
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is online now Online
5,785 posts
since Apr 2008
Mar 11th, 2009
1

Re: how to compare a string to an integer in java

An int is 4 bytes which is 32 bits (31 of which is value and the 32nd is a pos/neg toggle). A long is 8 bytes (63 bits data and 1 pos/neg toggle) so use long and parse that String to a long using Long.parseLong(string). If a long is also not large enough use the BigDecimal class. See the API docs.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Mar 11th, 2009
0

Re: how to compare a string to an integer in java

An integer in Java is a 32-bit number. If you need anything larger than that, I suggest you look at the Math.BigInteger class. Documentation for a BigInteger can be found here.
Last edited by darkagn; Mar 11th, 2009 at 6:48 am. Reason: Link error
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Mar 11th, 2009
0

Re: how to compare a string to an integer in java

Looks like James, masijade and I all posted at the same time, and I forgot about the long type Use that as masijade said.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Mar 11th, 2009
0

Re: how to compare a string to an integer in java

and since you're working with String-objects, make sure they're valid numeric values before you try to compare them
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,497 posts
since Jan 2007
Mar 11th, 2009
0

Re: how to compare a string to an integer in java

Ach, yeah, and as darkagn say, BigInteger, not BigDecimal (unless you're going to have decimals, in which case you'd have been using float and double instead of int and long, anyway).
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Mar 11th, 2009
0

Re: how to compare a string to an integer in java

Yes, long is necessary if your values can exceed 2^31 (although I suspect your example loop was just an example; you don't really want to execute a loop that many times!).
If your String isn't valid for converting to integer (or long) you'll get a NumberFormatException thrown.
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is online now Online
5,785 posts
since Apr 2008
Mar 11th, 2009
0

Re: how to compare a string to an integer in java

thanks for the fast responses!!!!

but is their any other methods other than big integers...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
koolhoney07 is offline Offline
7 posts
since Mar 2009
Mar 11th, 2009
0

Re: how to compare a string to an integer in java

What do u need exactly?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chanlee is offline Offline
1 posts
since Mar 2009

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: String search methods indexOf and lastIndexOf
Next Thread in Java Forum Timeline: playlist for media player





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


Follow us on Twitter


© 2011 DaniWeb® LLC