954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to compare a string to an integer in java

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

koolhoney07
Newbie Poster
7 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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 .

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

Looks like James, masijade and I all posted at the same time, and I forgot about the long type :$ Use that as masijade said.

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

and since you're working with String-objects, make sure they're valid numeric values before you try to compare them :)

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

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

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

thanks for the fast responses!!!!

but is their any other methods other than big integers...

koolhoney07
Newbie Poster
7 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

What do u need exactly?

chanlee
Newbie Poster
1 post since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

r u talking ab integers of more than 10 bytes ..like 12345678910228377466464678262737867865786826868326???

lipun4u
Newbie Poster
17 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Leave your lame chat-speak at the door. Read the forum rules on this.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Are you talking about integers of more than 10 bytes ..like 12345678910228377466464678262737867865786826868326???

@Ezzaral
Are you happy ???

lipun4u
Newbie Poster
17 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

I guess yes, but he would have been much happier if you would have dropped the multiple character typing (???) habit of yours too. That too is a symbol of chat/IM speak.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

Are you talking about integers of more than 10 bytes ..like 12345678910228377466464678262737867865786826868326???

@Ezzaral Are you happy ???


Yes, much better. That wasn't so hard, was it?

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Thank you

lipun4u
Newbie Poster
17 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 
r u talking ab integers of more than 10 bytes ..like 12345678910228377466464678262737867865786826868326???

yea i need for more than 10 byete as what u said...i.e more than or equal to 500 digits...

i.e my string is like this .....String (1234455....upto >=500 digits)

now i want compare that wid an integer in a for loop as already said

for(i=0;i< String (1234455....upto >=500 digits);i++)

koolhoney07
Newbie Poster
7 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

I would like to ask you the reason that you are comparing it to an integer.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 
I would like to ask you the reason that you are comparing it to an integer.

am writing a code for division for more than 512 bits...so in that i need to compare the value 'i' upto the value of the String.

so for that reason am asking u is their any special methods ??

koolhoney07
Newbie Poster
7 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

So is it that you are just interested in finding out whether the string contains all digits and not any other characters ? If yes you can use regex to do that, if not detail out the process.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You