Click here to Skip to main content
15,911,707 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The following is a description of the problem : Validate if a given string is numeric.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true


The algorithm I have so far is the following:

C#
public static boolean isNumber(String s) {

        if(s == null){
            return false;
        }
        s = s.toLowerCase();
        s = s.trim();
        if(s == "e" || s == "." || s == "/"){
            return false;
        }

       char[] myObject = new char[]{'0','1','2','3','4','5','6','7','8','9','.','/','e'};
        char[] myChar = s.toCharArray();

        for (int i = 0; i < myChar.length; i++) {

                int temp = 0;
               while(myChar[i] != myObject[temp])
               {
                   temp++;
                   if(temp == myObject.length){
                       return false;
                   }
               }



       }

        return true;
       }


When I used "e" as an input in my local machine it returns false which is correct; however, when I test the same code in LeetCode website it returns true. I can not figure out why this is happening. Can anyone help me please??? Here is a copy of leetcode's website

VB
Input:  "e"
Output: true
Expected:   false
Posted
Updated 28-Oct-14 11:49am
v2
Comments
Richard MacCutchan 28-Oct-14 13:36pm    
Go and ask the people at LeetCode.
Alexander24 28-Oct-14 13:37pm    
I already asked them,is really hard to get an email back :(
Richard MacCutchan 28-Oct-14 14:10pm    
The point is, how do you expect members of CodeProject to know why some other website is not correct?
[no name] 28-Oct-14 15:25pm    
"e" allone will be acpted? Maybe they accept "e" as the base of natural logarithm?
Sergey Alexandrovich Kryukov 28-Oct-14 17:50pm    
I renamed the title of your question, to attract more experts to it. I did not look for your post for a long time, because your title scared me off. Avoid using unrelated words in the titles of your question, otherwise many readers will never see your question page.
—SA

1 solution

Your algorithm is really bad, very redundant. Besides, it is incorrect. According to you algorithm, for example, some strings containing only the characters '0' to '9' is always valid, but it cannot be a a valid number if it is too long.

You need a completely different approach. Simply call the function parseDouble with your input string: http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#parseDouble%28java.lang.String%29[^].

From the documentation referenced above, you can see that NullPointerException or NumberFormatException can be thrown. Catch them both and return false; if the exception wasn't caught, return true. On top of it, you may want to add the validation for the range of the parsed number.

But even this code would be redundant. You simply don't need a validation function in this case (unless you also validated the range or something else); you should better do two things at a time: validate and obtain the numeric value from a string. If you want to wrap it in some function which will show validation result or return a valid numeric value, do it.

—SA
 
Share this answer
 
v2
Comments
[no name] 28-Oct-14 18:11pm    
Hello Sergey
I think
"According to you algorithm, for example, some strings containing only the characters '0' to '9' is always valid, but it cannot be a a valid number if it is too long."
contains big potential to missunderstand for non english natives like me...at least I hope I interpreted it right ;)
Sergey Alexandrovich Kryukov 28-Oct-14 19:15pm    
For example, 897 is valid, but according to OP's algorithm, this is also valid:
943282570498570425974235972435092834750943827502349857230495782034958720349857342098573409587043985723408596234085972309809745723495734250938475034
But it would not parse as integer of any FPU numeric type (only as longint). :-)
Do you know how to explain it more clearly?
—SA
[no name] 28-Oct-14 19:23pm    
No, but at least that explains, that I interpreted your answer in the right way.

OP did not ask to convert to int (or whatever) he/she asked only IsNumber ;)
Sergey Alexandrovich Kryukov 28-Oct-14 19:28pm    
Yes, yes, but this is the whole point: it's pointless to validate if something is a number. It can be a number from one point of view and not a number from another.
Okay, another example: 1.1e99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 won't parse as a number of any floating point or integer type.
Even simpler, "1e2e3e" will pass OP's algorithm, too, and it cannot be any number at all.
The validation used by OP is simply wrong, from any point of view. But more important is the rationale: if something is validated as a number, it will be later parsed to a number of one or another type. There should not be later.
Hence my solution: just a few line of code. Parse methods already do validation.
—SA
[no name] 28-Oct-14 19:44pm    
I agree the OP's "isNumber" is far away from correct (one number more after your "1e2e3e" can be become again a discussion, whether it is correct or not, for me it is correct, even if it is uncommon)...
Kind regards, Bruno

But OP's question was about "e" which is still not clear for me, is it "e" allone like "e" (base Ln) or "e" in scientific notation of a number. And this for me unknown "LeetCode" accept it and he is asking why?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900