色www,五月婷婷深爱五月,午夜国产一级片,色噜噜综合,国产大胸无码视频,清纯美女被操黄网站在线观看,波多野结衣av高清一区二区三区

java字符串轉(zhuǎn)化整型問題

時間:2024-07-23 09:12:07 JAVA認證 我要投稿

java字符串轉(zhuǎn)化整型問題

  2016年JAVA認證考試已經(jīng)在緊張的備考中了,在后期中考來臨之際我們將會第一時間為廣大考生發(fā)布中考時間安排,請廣大考生隨時關(guān)注本站。下面是JAVA認證復習備考資料——java 字符串轉(zhuǎn)化整型問題。

  public class StringParesInteger {

  /**

  * @param args

  */

  public static void main(String[] args) {

  // TODO Auto-generated method stub

  System.out.println(Integer.MIN_VALUE);

  System.out.println(Integer.MAX_VALUE/10);

  System.out.println(pareseInt("="));

  }

  /**本題考查的主要是邊界條件

  * 1.穿入的字符串是否為空

  * 2.字符串的首位是否為(+、-)

  * 3.字符中是否有非法字符

  * 4.穿入的字符串是否超過了整數(shù)的最大值(Integer.MAX_VALUE(2147483647)/Integer.MIN_VALUE(-2147483648))

  *

  * @param data

  * @return

  */

  public static int pareseInt(String data){

  /*

  * 判讀穿傳入的字符串是否為空

  */

  if(data==null||data.length()==0){

  throw new NullPointerException("data is null");

  }

  int index=0;

  /**

  *

  */

  //是否為負數(shù)

  boolean isPositive=true;

  // 臨界值

  int limit = 0;

  //取出字符串的第一位

  char first=data.charAt(0);

  //第一位是負數(shù)的情況下

  if(first=='-'){

  isPositive=false;

  index++;

  //設(shè)置整形最小的負數(shù)(-2147483648)

  limit=-Integer.MIN_VALUE;

  }

  //第一位是整數(shù)的情況下

  if(first=='+'){

  isPositive=true;

  //設(shè)置最大的正數(shù)是(2147483647)

  limit=Integer.MAX_VALUE;

  index++;

  }

  //設(shè)置比較的邊界值(214748364)

  int maxLimit=Integer.MAX_VALUE/10;

  int length=data.length();

  int result=0;

  while(index'0'&&ch<'9'){

  //先判斷原來的值是否大于比較的臨界值

  if(result>maxLimit){

  throw new RuntimeException("整數(shù)越界了");

  }

  // 判斷當前位的值+ch的值是否》整數(shù)的最大值

  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("數(shù)組越界了s ");

  }

  index++;

  result=result*10+(ch-'0');

  }else{

  throw new RuntimeException("不是整數(shù) ");

  }

  }

  //三目運算符

  return isPositive?result:-result;

  }

  }

【java字符串轉(zhuǎn)化整型問題】相關(guān)文章:

Java認證基礎(chǔ)知識:java字符串轉(zhuǎn)化整型問題07-21

關(guān)于JAVA字符串的拼接與性能06-07

JAVA十六進制與字符串的轉(zhuǎn)換方法09-17

sun認證java關(guān)于字符串處理技巧06-01

sun認證考試java關(guān)于字符串處理技巧06-22

Java字符串反轉(zhuǎn)和Eclipse代碼自動提示11-06

Java常用字符串反轉(zhuǎn)的五種方案05-06

Java中將毫秒轉(zhuǎn)化為日期的方法09-19

Java多線程同步問題10-01

Java編程常見問題匯總10-05