Java String Conversion integer

Source: Internet
Author: User

Public class StringParesInteger {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stubSystem. out. println (Integer. MIN_VALUE); System. out. println (Integer. MAX_VALUE/10); System. out. println (pareseInt ("=");}/** the main boundary condition * 1 is used in this question. whether the input string is null * 2. whether the first character string is (+,-) * 3. whether the characters contain invalid characters * 4. whether the input string exceeds the maximum Integer value (Integer. MAX_VALUE (1, 2147483647)/Integer. min _value (-2147483648) ** @ param data * @ return */public static int pareseInt (String data) {/** determine whether the passed string is null */if (data = null | data. length () = 0) {throw new NullPointerException ("data is null");} int index = 0; /***** // whether it is negative boolean isPositive = true; // The Critical Value int limit = 0; // retrieves the first char first = data of the string. charAt (0); // if (first = '-') {isPositive = false; index ++; // set the minimum Integer negative (-2147483648) limit =-Integer. MIN_VALUE;} // if (first = '+') {isPositive = true; // set the maximum positive value to (2147483647) limit = Integer. MAX_VALUE; index ++;} // set the Compare boundary value (214748364) int maxLimit = Integer. MAX_VALUE/10; int length = data. length (); int result = 0; while (index
 
  
'0' & ch <'9') {// first, judge whether the original value is greater than the comparison threshold. if (result> maxLimit) {throw new RuntimeException ("integer out of bounds ");} // determine whether the value of the current bit + the value of ch is the maximum integer if (result * 10> limit-(ch-'0') {System. out. println ("result -->" + (result * 10); System. out. println ("max ----->" + (Integer. MAX_VALUE); System. out. println ("result -->" + (Integer. MAX_VALUE-(ch-'0') + "ch =" + (ch-'0'); throw new RuntimeException ("array out of bounds s ");} index ++; result = result * 10 + (ch-'0');} else {throw n Ew RuntimeException ("not an integer");} // return isPositive? Result:-result ;}}
 

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.