A string representing a numeric value (Java implementation)

Source: Internet
Author: User

Implement a function to determine whether a string represents a numeric value (including integers and decimals). For example, the string "+100", "5e2", "123", "3.1416" and " -1E-16" all represent numeric values. But "12e", "1a3.14", "1.2.3", "+-5" and "12e+4.3" are not.

1  Public classDemo2 {2 3     //array subscript member variable4     intindex; 5   6      Public BooleanIsnumeric_2 (Char[] str) {  7         //Input Exception8         if(str = =NULL)  9             return false; Tenindex = 0;  One         //start with a positive sign A         if(Str[index] = = ' + ' | | str[index] = = '-')   -index++;  -         if(Index = =str.length) the             return false;  -         //set numeric to determine whether a number -         Booleannumeric =true;  - scandigits (str);  +         if(Index! =str.length) { -             //decimal +             if(Str[index] = = '. ') {   Aindex++;  at scandigits (str);  -                 if(Index < Str.length && (str[index] = = ' E ' | | str[index] = = ' E '))   -numeric =isexponential (str);  -}Else if(Str[index] = = ' E ' | | str[index] = = ' E ')   -numeric =isexponential (str);  -             Else   in                 //An exception character has occurred -numeric =false;  to         }   +    -         returnNumeric && Index = =str.length;  the     }   *    $     //Scan an array, if the current character is a number, index++Panax Notoginseng     Private voidScandigits (Char[] str) {   -          while(Index < str.length && Str[index] >= ' 0 ' && Str[index] <= ' 9 ')   theindex++;  +     }   A    the     //determines whether the end of a numeric value represented by scientific notation +     Private BooleanIsexponential (Char[] str) {   -         if(Str[index]! = ' E ' && str[index]! = ' E ')   $             return false;  $index++;  -         if(Index = =str.length) -             return false;  the         if(Str[index] = = ' + ' | | str[index] = = '-')   -index++; Wuyi         if(Index = =str.length) the             return false;  - scandigits (str);  Wu         //If there are special characters, index will not be str.length -         returnindex = = str.length?true:false;  About     }   $}

Reprinted from: http://blog.csdn.net/zjkc050818/article/details/72818475

A string representing a numeric value (Java implementation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.