亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

.split("\") 不起作用,并且還有一個錯誤 arrayIndexOitOfBounds

.split("\") 不起作用,并且還有一個錯誤 arrayIndexOitOfBounds

慕碼人2483693 2022-11-02 17:29:16
String dateofbirth = ((JTextField)jDateChooser1.getDateEditor().getUiComponent()).getText();        System.out.println(""+dateofbirth);        String [] dob= dateofbirth.split("/");       System.out.println(""+dob[0]);       System.out.println(""+dob[1]);       System.out.println(""+dob[2]);
查看完整描述

3 回答

?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

您需要檢查 dateofbirth 格式是否正確,并通過檢查數組長度來防止異常。


String [] dob= dateofbirth.split("/");


if(dob != null && dob.length >=3){

       System.out.println(""+dob[0]);

       System.out.println(""+dob[1]);

       System.out.println(""+dob[2]);

}


查看完整回答
反對 回復 2022-11-02
?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

似乎數組 dob 只有一個元素,其中沒有索引 1。這就是為什么您會看到java.lang.ArrayIndexOutOfBoundsException: 1 索引從 0 開始。


使用循環來導航數組,以便您可以根據數組大小動態處理用例。舉個例子,見下文。


例子


        String input = "abc/def/ghi/jkl";

        String[] matrix = input.split("/");


        /* Print each letter of the string array in a separate line. */

        for(int i = 0; i < matrix.length; ++i) {

             System.out.println(matrix[i]);

        }

這將給出如下輸出,


abc

def

ghi

jkl

這樣可以避免遇到java.lang.ArrayIndexOutOfBoundsException:


查看完整回答
反對 回復 2022-11-02
?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

您應該使用數組索引越界異常嘗試捕獲。


try {

      String [] dob= dateofbirth.split("/");

       System.out.println(""+dob[0]);

       System.out.println(""+dob[1]);

       System.out.println(""+dob[2])


catch(ArrayIndexOutOfBoundsException exception) {

    handleTheExceptionSomehow(exception);

}


查看完整回答
反對 回復 2022-11-02
  • 3 回答
  • 0 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號