[Sword refers to Offer learning] [interview question 54: a string that represents a value], sword refers to offer

Source: Internet
Author: User

[Sword refers to Offer learning] [interview question 54: a string that represents a value], sword refers to offer
Question: Use a function to determine whether a string represents a number (including an integer and a decimal number ).Example

For example, strings "+ 100", "5e2", "-123", "3.1416", and "-1E-16" all represent numerical values, but "12e", "1a3. 14 "," 1.2.3 "," +-5 "and" 12e + 5.4 "are none.
  

Solutions

Before a value, there may be a '-' or '+' that indicates plus or minus the value '. Next, several digits ranging from 0 to 9 represent the integer part of the value (in some decimal places, there may be no integer part of the value ). If the value is a decimal point, there may be a number of digits ranging from 0 to 9 after the decimal point to indicate the decimal part of the value. If the numeric value is represented by scientific notation, then an 'e' or 'E', and an integer that follows (can have a positive or negative number) to represent the index.
When determining whether a string meets the preceding pattern, first check whether the first character is a plus or minus sign. If yes, move one character on the string and continue scanning the digits 0 to 9 in the remaining string. If it is a decimal point, a decimal point is displayed. In addition, if it is a numerical value expressed by scientific notation, 'E' or 'e' may be encountered after an integer or decimal number '.

Code Implementation
Public class Test54 {/*** title: implement a function to determine whether a string represents a value (including an integer and a decimal number ). ** @ Param string * @ return */public static boolean isNumeric (String string) {if (string = null | string. length () <1) {return false;} int index = 0; if (string. charAt (index) = '+' | string. charAt (index) = '-') {index ++;} // if (index> = string. length () {return false;} boolean numeric = true; index = scanDigits (string, index); // if (index <string. length ()){/ /If it is the decimal point if (string. charAt (index) = '. ') {// move to the next position index ++; index = scanDigits (string, index); // if (index> = string. length () {numeric = true;} // The end position of the string is not reached yet. else if (index <string. length () & (string. charAt (index) = 'E' | string. charAt (index) = 'E') {numeric = isExponential (string, index) ;}else {numeric = false ;}} // else if (string. charAt (index) = 'E' | String. charAt (index) = 'E') {numeric = isExponential (string, index);} else {numeric = false;} return numeric ;} // The end of the string is reached, indicating that there is no exponential else {return true ;}}/*** to determine whether it is the end of the scientific notation, such as E5, e5, E + 5, E-5, e (E) followed by an integer ** @ param string * @ param index start position * @ return matched result */private static boolean isExponential (String string, int index) {if (index> = string. length () | (string. charAt (index )! = 'E' & string. charAt (index )! = 'E') {return false;} // move to the next position to be processed index ++; // when the string ends, false if (index> = string. length () {return false;} if (string. charAt (index) = '+' | string. charAt (index) = '-') {index ++;} // when the string ends, false if (index> = string. length () {return false;} index = scanDigits (string, index); // if the number has been processed, return index> = string. length ();} /*** scan the numeric part of the string ** @ param string * @ param index start position * @ return the position of the first numeric character starting from the scanning position */private static int scanDigits (String string, int index) {while (index <string. length () & string. charAt (index)> = '0' & string. charAt (index) <= '9') {index ++;} return index;} public static void main (String [] args) {System. out. println (isNumeric ("100") + "[" + true + "]"); System. out. println (isNumeric ("123.45e + 6") + "[" + true + "]"); System. out. println (isNumeric ("+ 500") + "[" + true + "]"); System. out. println (isNumeric ("5e2") + "[" + true + "]"); System. out. println (isNumeric ("3.1416") + "[" + true + "]"); System. out. println (isNumeric ("600. ") +" ["+ true +"] "); System. out. println (isNumeric ("-. 123 ") +" ["+ true +"] "); System. out. println (isNumeric ("-1E-16") + "[" + true + "]"); System. out. println (isNumeric ("100") + "[" + true + "]"); System. out. println (isNumeric ("1.79769313486232E + 308") + "[" + true + "]"); System. out. println (); System. out. println (isNumeric ("12e") + "[" + false + "]"); System. out. println (isNumeric ("1a3. 14 ") +" ["+ false +"] "); System. out. println (isNumeric ("1 + 23") + "[" + false + "]"); System. out. println (isNumeric ("1.2.3") + "[" + false + "]"); System. out. println (isNumeric ("+-5") + "[" + false + "]"); System. out. println (isNumeric ("12e + 5.4") + "[" + false + "]");}
Running result

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.