943,724 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 7808
  • Java RSS
You are currently viewing page 3 of this multi-page discussion thread; Jump to the first page
Mar 12th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by verruckt24 ...
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??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
koolhoney07 is offline Offline
7 posts
since Mar 2009
Mar 12th, 2009
0

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

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.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Mar 12th, 2009
0

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

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.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Mar 12th, 2009
0

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

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

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

convert i to String and use compareTo() function

here is how it works
Quote ...

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));
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lipun4u is offline Offline
17 posts
since Feb 2009
Mar 12th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by lipun4u ...
convert i to String and use compareTo() function

here is how it works
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Mar 12th, 2009
0

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

Anyways it gives the same outcome which is "-1"
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Mar 12th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by verruckt24 ...
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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Mar 12th, 2009
0

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

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.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Mar 12th, 2009
1

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

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