I need to check whether the String is an Integer. And I found such solution:  private boolean isInteger(String str) {     try {         Integer.parseInt(str);         return true;     } catch (NumberFormatException e) {         return false;     } }   Is there a more beautiful and proper way to do this without try/catch clause?
57572
I normally use the following idiom to check if a String can be converted to an integer.  public boolean isInteger( String input ) {     try {         Integer.parseInt( input );         return true;     }     catch( Exception e ) {         return false;     } }   Is it just me, or does this seem a bit hackish?  What's a better way?    See my answer (with benchmarks, based on the  by ) to see why I've reversed my position and accepted  to this problem.  I think this original code will be used by most people because it's quicker to implement, and more maintainable, but it's orders of magnitude slower when non-integer data is provided.
79610
an example code what I try to do will surely do better than my english:  public bool IsNumericValueInBounds (string value, Type numericType) {   double d = double.NaN;         bool inBounds = (bool)numericType.GetMethod ("TryParse").Invoke (null, new object[] { value, d });    return inBounds; }   Unfortunately the TryParse method needs an out parameter so this doesn't work. any ideas how to solve this?  (ps.: would'nt this be a nice example for duck typing? - because i know every numericType has an "TryParse" or I am mistaken?)
93
I've been using the == operator in my program to compare all my strings so far. However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug.  Is == bad? When should it and should it not be used? What's the difference?
123205
Is there a simple way in Perl that will allow me to determine if a given variable is numeric?  Something along the lines of:  if (is_number($x)) { ... }   would be ideal.  A technique that won't throw warnings when the -w switch is being used is certainly preferred.
117335
I'm trying to check if an input is an integer and I've gone over it a hundred times but don't see the error in this. Alas it does not work, it triggers the if statement for all inputs (numbers/letters)  read scale if ! [[ "$scale" =~ "^[0-9]+$" ]]         then             echo "Sorry integers only" fi   I've played around with the quotes but either missed it or it did nothing. What do I do wrong? Is there an easier way to test if an input is just an INTEGER?
78012
How do I determine if a number is prime?  I'm writing a program where a user inputs any integer and from that the program determines whether the number is prime, but how do I go about that?
116653
I have this:  If String.IsNullOrEmpty(editTransactionRow.pay_id.ToString()) = False Then     stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value End If   Now, when editTransactionRow.pay_id is Null Visual Basic throws an exception. Is there something wrong with this code?
37105
I am using java language,I have a method that is supposed to return an object if it is found. If it is not found, should I:  return null throw an exception other  Which is the best practise or idiom?
684
Is there any way to check whether an integer is a member of a list of integer numbers and return a boolean?   I found this neat solution,  that does a great job. It checks whether a string exists in a list of strings:   \documentclass{article} \usepackage{xstring} \newcommand\IfStringInList[2]{\IfSubStr{,#2,}{,#1,}} \begin{document}     \IfStringInList{Paul}{George,John,Paul,Ringo}{True}{False} \end{document}    However, it does return a string not a boolean "true" or "false" and caused me some difficulty which is posted .    Let's call the macro I would like to have \ISMEMBER. My goal is to use \ISMEMBER and examine whether an integer is in the list and depending on whether it is or not, perform some tasks, as an example:  \fpeval{ \ISMEMBER{1}{1,2,3,4,5} ? \MACRO_FOR_MEMBERS : \MACRO_FOR_NON_MEMBERS }   Is such functionality possible?
2156
In Java is there a way to check the condition:  "Does this single character appear at all in string x"  without using a loop?
87194
I've been told that code such as:  for (int i = 0; i &lt; x.length(); i++) {     // blah }   is actually O(n^2) because of the repeated calls to x.length(). Instead I should use:  int l = x.length(); for (int i = 0; i &lt; l; i++) {     // blah }   Is this true? Is string length stored as a private integer attribute of the String class? Or does String.length() really walk the whole string just to determine its length?
24951
I wrote a function in C that converts a string to an integer and returns the integer. When I call the function I also want it to let me know if the string is not a valid number. In the past I returned -1 when this error occurred, because I didn't need to convert strings to negative numbers. But now I want it to convert strings to negative numbers, so what is the best way to report the error?  In case I wasn't clear about this: I don't want this function to report the error to the user, I want it to report the error to the code that called the function. ("Report" might be the wrong word to use...)  Here's the code:  s32 intval(const char *string) {     bool negative = false;     u32 current_char = 0;      if (string[0] == '-') {         negative = true;         current_char = 1;     }      s32 num = 0;     while (string[current_char]) {         if (string[current_char] &lt; '0' || string[current_char] &gt; '9') {             // Return an error here.. but how?         }          num *= 10;         num += string[current_char] - '0';         current_char++;     }      if (negative) {         num = -num;     }      return num; }
2827
What's the cleanest, most effective way to validate decimal numbers in JavaScript?  Bonus points for:   Clarity. Solution should be clean and simple. Cross-platform.   Test cases:  01. IsNumeric('-1')      =&gt; true 02. IsNumeric('-1.5')    =&gt; true 03. IsNumeric('0')       =&gt; true 04. IsNumeric('0.42')    =&gt; true 05. IsNumeric('.42')     =&gt; true 06. IsNumeric('99,999')  =&gt; false 07. IsNumeric('0x89f')   =&gt; false 08. IsNumeric('#abcdef') =&gt; false 09. IsNumeric('1.2.3')   =&gt; false 10. IsNumeric('')        =&gt; false 11. IsNumeric('blah')    =&gt; false
24404
Is there any way to determine (programatically, of course) if a given pointer is "valid"? Checking for NULL is easy, but what about things like 0x00001234? When trying to dereference this kind of pointer an exception/crash occurs.  A cross-platform method is preferred, but platform-specific (for Windows and Linux) is also ok.  Update for clarification: The problem is not with stale/freed/uninitialized pointers; instead, I'm implementing an API that takes pointers from the caller (like a pointer to a string, a file handle, etc.). The caller can send (in purpose or by mistake) an invalid value as the pointer. How do I prevent a crash?
103793
I have a generic class that should allow any type, primitive or otherwise. The only problem with this is using default(T). When you call default on a value type or a string, it initializes it to a reasonable value (such as empty string). When you call default(T) on an object, it returns null. For various reasons we need to ensure that if it is not a primitive type, then we will have a default instance of the type, not null. Here is attempt 1:  T createDefault() {     if(typeof(T).IsValueType)     {         return default(T);     }     else     {         return Activator.CreateInstance&lt;T&gt;();     } }   Problem - string is not a value type, but it does not have a parameterless constructor. So, the current solution is:  T createDefault() {     if(typeof(T).IsValueType || typeof(T).FullName == "System.String")     {         return default(T);     }     else     {         return Activator.CreateInstance&lt;T&gt;();     } }   But this feels like a kludge. Is there a nicer way to handle the string case?
7172
What would be the best way to look in a string[] to see if it contains a element. This was my first shot at it. But perhaps there is something that I am overlooking. The array size will be no larger than 200 elements.  bool isStringInArray(string[] strArray, string key) {     for (int i = 0; i &lt;= strArray.Length - 1; i++)         if (strArray[i] == key)             return true;     return false; }
80089
I have seen a solution that I don't get to work - I need to change data type from string to integer and I did what is shown i the image below Can someone point out what I do wrong?