how to compare a string to an integer in java

Reply

Join Date: Mar 2009
Posts: 7
Reputation: koolhoney07 is an unknown quantity at this point 
Solved Threads: 0
koolhoney07 koolhoney07 is offline Offline
Newbie Poster

how to compare a string to an integer in java

 
0
  #1
Mar 11th, 2009
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++)
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark

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

 
0
  #2
Mar 11th, 2009
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");
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,355
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: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

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

 
1
  #3
Mar 11th, 2009
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.
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: Aug 2007
Posts: 791
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

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

 
0
  #4
Mar 11th, 2009
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
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 791
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

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

 
0
  #5
Mar 11th, 2009
Looks like James, masijade and I all posted at the same time, and I forgot about the long type Use that as masijade said.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

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

 
0
  #6
Mar 11th, 2009
and since you're working with String-objects, make sure they're valid numeric values before you try to compare them
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,355
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: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

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

 
0
  #7
Mar 11th, 2009
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).
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: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark

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

 
0
  #8
Mar 11th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 7
Reputation: koolhoney07 is an unknown quantity at this point 
Solved Threads: 0
koolhoney07 koolhoney07 is offline Offline
Newbie Poster

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

 
0
  #9
Mar 11th, 2009
thanks for the fast responses!!!!

but is their any other methods other than big integers...
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 1
Reputation: chanlee is an unknown quantity at this point 
Solved Threads: 0
chanlee chanlee is offline Offline
Newbie Poster

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

 
0
  #10
Mar 11th, 2009
What do u need exactly?
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC