java字符串轉化整型問題

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

    java字符串轉化整型問題

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

      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.穿入的字符串是否超過了整數的最大值(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;

      /**

      *

      */

      //是否為負數

      boolean isPositive=true;

      // 臨界值

      int limit = 0;

      //取出字符串的第一位

      char first=data.charAt(0);

      //第一位是負數的情況下

      if(first=='-'){

      isPositive=false;

      index++;

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

      limit=-Integer.MIN_VALUE;

      }

      //第一位是整數的情況下

      if(first=='+'){

      isPositive=true;

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

      limit=Integer.MAX_VALUE;

      index++;

      }

      //設置比較的邊界值(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("整數越界了");

      }

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

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

      }

      index++;

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

      }else{

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

      }

      }

      //三目運算符

      return isPositive?result:-result;

      }

      }

    【java字符串轉化整型問題】相關文章:

    Java認證基礎知識:java字符串轉化整型問題07-21

    關于JAVA字符串的拼接與性能06-07

    JAVA十六進制與字符串的轉換方法09-17

    sun認證java關于字符串處理技巧06-01

    sun認證考試java關于字符串處理技巧06-22

    Java字符串反轉和Eclipse代碼自動提示11-06

    Java常用字符串反轉的五種方案05-06

    Java中將毫秒轉化為日期的方法09-19

    Java多線程同步問題10-01

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

    91久久大香伊蕉在人线_国产综合色产在线观看_欧美亚洲人成网站在线观看_亚洲第一无码精品立川理惠

      中文字字幕国产精品 | 五月丁香视频国产精 | 在线播放一区二区不卡三区 | 日本三级香港三级乳网址 | 亚洲国产天堂女人午夜看片 | 在线观看黄aⅴ免费观看 |