how to compare a string to an integer in java

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
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

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

 
0
  #21
Mar 12th, 2009
Originally Posted by verruckt24 View Post
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.
yea the string contains only integer values....what is regex???and how to use it??
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

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

 
0
  #22
Mar 12th, 2009
Here is the tutorial for Regular Expressions (regex is short for them) you will get to know a lot about them and how to use them on the link.

To just provide a brief about what a regex is and how they would be useful in such operations, take the current example where you just want to check wheter a string is an integer, in other words you want to check whether it contains just digits or other characters too.
So you could define a regex with a pattern such as [\\d*] Here '\\d' means digits (0-9), and * means any number of occurrences of them. After such a pattern has been defined, you just check whether the string satisfies this pattern or not, any string satisfying this pattern would certainly contain only digits and hence would classify to be an integer.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

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

 
0
  #23
Mar 12th, 2009
I'm still not sure what you are asking, are you trying to determine if each position in a given string is a digit (numeric)? If so then what was stated above will do just fine. If not what is the purpose of comparing a gigantor number such as xyz...512 characters to an integer.

In your example loop you would simply loop up to the size of the the string and not necessarily the position.

IE: if you need to compare each position in a string to determine if it is in fact a digit you would want something that loops up to the string.length and then use the [] accessors to determine if a specific point is a digit IE: is string[i] a digit.
Last edited by Killer_Typo; Mar 12th, 2009 at 3:49 am.
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,461
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: 263
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
  #24
Mar 12th, 2009
If you need a mathematical comparison, and the numbers are too large to store in a long, then no, there is no other (uncomplicated) way than with BigInteger. Now, if you think you must do it some other way, than convert the int that you are to compare it to, to a String, than check the lengths of the String. The longer one is the larger. If they are same, start taking one character at time from the front of each String and comparing them (Strings compareTo is good enough for this). As soon as you find one that is larger than the other, you've found the larger number. Now, is it wise to do it this way? No. It is wise to use BigInteger, as already said.

But, as asked once already, what is it that you are actually trying to acheive? If you are attempting to loop some 1000000000000 times, than be prepared to wait, no matter what method you use. And, if you are trying to loop that many times, than the real question is why? What are you doing that you feel you need to loop that long?
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: Feb 2009
Posts: 12
Reputation: lipun4u is an unknown quantity at this point 
Solved Threads: 0
lipun4u lipun4u is offline Offline
Newbie Poster

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

 
0
  #25
Mar 12th, 2009
convert i to String and use compareTo() function

here is how it works

public class StrCmp {
public static void main(String args[]) {
String s1 = new String("1234567891011");
String s2 = new String("1234567891012");
System.out.println(s1.compareTo(s2));
}
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,461
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: 263
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
  #26
Mar 12th, 2009
Originally Posted by lipun4u View Post
convert i to String and use compareTo() function

here is how it works
  1. public class StrCmp {
  2. public static void main(String args[]) {
  3. String s1 = new String("1234567891011");
  4. String s2 = new String("1234567891012");
  5. System.out.println(s1.compareTo(s2));
  6. }
  7. }
Try it with
  1. public class StrCmp {
  2. public static void main(String args[]) {
  3. String s1 = new String("1234567891011");
  4. String s2 = new String("234567891012");
  5. System.out.println(s1.compareTo(s2));
  6. }
  7. }

You might be surprised by the outcome.
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: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

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

 
0
  #27
Mar 12th, 2009
Anyways it gives the same outcome which is "-1"
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,461
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: 263
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
  #28
Mar 12th, 2009
Originally Posted by verruckt24 View Post
Anyways it gives the same outcome which is "-1"
Which is saying that s1 is less than s2. Well, is it? ;-)
Last edited by masijade; Mar 12th, 2009 at 7:44 am.
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: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

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

 
0
  #29
Mar 12th, 2009
Yes you are right.

@lipun4 : Read what the docs for compareTo() have to say to understand the point masijade makes here cleverly.
Last edited by verruckt24; Mar 12th, 2009 at 8:38 am.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,461
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: 263
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
  #30
Mar 12th, 2009
Like I said, though, "-1" says that s1 (which is the lesser number the first time, but the greater number the second time) is less than s2, whereby s1 is significantly greater than s2 the second time, not less than s2 as "-1" indicates.

What I am saying, is that you cannot String compare the entire String at once. That comparison is done alphabetically, not numerically. For single characters, alphabetically and numerically are the same, but not for multicharacter Strings. (E.G. alphabetically 10 is less than 2).
Last edited by masijade; Mar 12th, 2009 at 8:41 am.
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:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC