| | |
how to compare a string to an integer in java
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2009
Posts: 7
Reputation:
Solved Threads: 0
yea the string contains only integer values....what is regex???and how to use it??
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.
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 !!!
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.
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!
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?
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
----------------------------------------------
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
•
•
•
•
convert i to String and use compareTo() function
here is how it works
Java Syntax (Toggle Plain Text)
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)); } }
Java Syntax (Toggle Plain Text)
public class StrCmp { public static void main(String args[]) { String s1 = new String("1234567891011"); String s2 = new String("234567891012"); System.out.println(s1.compareTo(s2)); } }
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
----------------------------------------------
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
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
----------------------------------------------
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
Yes you are right.
@lipun4 : Read what the docs for
@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 !!!
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).
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
----------------------------------------------
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
![]() |
Similar Threads
- Need help with T/F and Multiple Choice (Java)
- Searching/Comparing a word in a stack (Java)
- wierd gui/constructor problem (Java)
- java I/O (JSP)
- Creating a "Data" folder in my C: Drive using my Java program (Java)
- Delete button not working on my Java GUI Inventory (Java)
- a for loop problem (Java)
- Something about String.split("-"); problem (Java)
- Need Help with ArrayList sorting (Java)
Other Threads in the Java Forum
- Previous Thread: String search methods indexOf and lastIndexOf
- Next Thread: playlist for media player
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component csv database desktop draw ebook eclipse equation error event exception fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect iphone j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions reporting rotatetext scanner screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream






