【问题标题】:Getting ArrayIndexOutOfBoundException while accessing string array访问字符串数组时获取 ArrayIndexOutOfBoundException
【发布时间】:2019-03-08 07:26:03
【问题描述】:

我正在尝试拆分字符串并将其与常量字符串匹配。我得到了我想检查发件人的地址字符串,我试图拆分字符串。为此,我尝试了这种方式。

if (cursor != null) {
 cursor.moveToFirst();
 try {
  while (!cursor.isAfterLast() && !cursor.isBeforeFirst()) {
   String messageAddress = cursor.getString(cursor.getColumnIndexOrThrow("address"));
   if (isCorporateMessage(messageAddress)) {
    String matchString[] = messageAddress.split("-");
    if (matchString[0].length() == 2) { //getting exception here I guess
     if (matchString[1].toUpperCase().contains("OLA")) {
      isFrom = 1;
     } else if (matchString[1].toUpperCase().contains("UBR")) {
      isFrom = 2;
     } else if (matchString[1].toUpperCase().contains("BK")) {
      isFrom = 3;
     } else if (Utils.isBankMessage(matchString[1].toUpperCase())) {
      isFrom = 3;
     } else if (cursor.getString(cursor.getColumnIndexOrThrow("body")).toUpperCase().contains("BANK")) {
      isFrom = 3;
     } else if (matchString[1].toUpperCase().contains("PAYTM")) {
      isFrom = 4;
     } else if (matchString[2].toUpperCase().contains("VK-iPaytm") || matchString[2].toUpperCase().contains("AD-IPAYTM")) {
      isFrom = 4;
     } else {
      isFrom = 5;
     }
     // check if message has otp or not
     String smsBody = cursor.getString(cursor.getColumnIndexOrThrow("body"));
     if (!Utils.isContainAnyTypeOfCode(smsBody)) {

      SMSModel sms = new SMSModel();

      sms.setId(messageAddress);
      sms.setBody(cursor.getString(cursor.getColumnIndexOrThrow("body")).toString());
      sms.setAddress(cursor.getString(cursor.getColumnIndexOrThrow("address")).toString());


      sms.setType(String.valueOf(isFrom));
      long date = cursor.getLong(cursor.getColumnIndex("date"));

      SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
      String dateString = formatter.format(new Date(date));
      sms.setDate(dateString);

      if (isFrom > 0) {
       CounterHolder.smsModelArrayList.add(cursor.getString(cursor.getColumnIndexOrThrow("body")).toString());

       if (!datePreference.getBoolean("firstDump", false)) {

        sms.setBody(cursor.getString(cursor.getColumnIndexOrThrow("body")));
        CounterHolder.smsPromArrayList.add(sms);

        Log.d("firstDump", "Processed");

       } else if (datePreference.getBoolean("firstDump", false)) {
        SimpleDateFormat fmtOut = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);

        if (Utils.isLatestSMS(context, fmtOut.format(new Date(date)))) {
         Log.d("sms", "after first dump");

         sms.setBody(cursor.getString(cursor.getColumnIndexOrThrow("body")));
         CounterHolder.smsPromArrayList.add(sms);
        } else {
         Log.d("sms", "before first dump");
        }
       }
      }
     }
    }
   }
   cursor.moveToNext();
  }

  cursor.close();
  Log.d("myapp", "Count of sms" + CounterHolder.smsPromArrayList.size());

  return true;

 } catch (Exception e) {
  e.printStackTrace();
 }

现在,它抛出 java.lang.ArrayIndexOutOfBoundsException: length=2;索引 = 2 异常。如何在不出现异常的情况下拆分字符串并匹配?

请帮忙。谢谢你。。

【问题讨论】:

  • 确切的错误信息是什么?
  • matchString[2] - 也许这应该是matchString[1]
  • 只有 2 个索引 matchString[0]matchString[1] 并且您要访问第三个索引 matchString[2]
  • else if (matchString[2].toUpperCase().contains("VK-iPaytm") || matchString[2].toUpperCase().contains("AD-IPAYTM")) { isFrom = 4;这是不正确的 matchString[2] 对两个元素的数组无效。
  • 等等,你用“-”分割你的数组,所以matchString[2].toUpperCase().contains("VK-iPaytm")这永远不会返回真。另外,您正在转换为大写字母,并且“VK-iPaytm”具有小写字母,因此也会出现问题。 (只是您以后可能会遇到的问题)

标签: java android arrays string indexoutofboundsexception


【解决方案1】:

访问数组索引时,您必须确保该索引有效。如果您的地址是BX-262735,那么您的数组大小为 2。因此,请在使用前检查有效的数组索引。

  if ( matchString.length > 2 &&  matchString[2].toUpperCase().contains("some string") ) {
     //.....
   }

【讨论】:

  • } else if (matchString.length > 2 && matchString[2].toUpperCase().contains("VK-iPaytm") || matchString[2].toUpperCase().contains("AD -IPAYTM")) 我试过这个仍然得到同样的异常
【解决方案2】:

"BX-262735 with""-" 上拆分将产生一个包含两个元素的数组,但带有matchString[2]

您正在尝试访问该数组的第三个元素。这就是您的错误消息告诉您的内容。

【讨论】:

    【解决方案3】:

    像这样更改或根据您的概念检查您的数组拆分是否正确

    else if (matchString.length > 2 &&   
    matchString[2].toUpperCase().contains("VK-iPaytm") || 
    matchString[2].toUpperCase().contains("AD-IPAYTM")) {
    isFrom = 4;
    }
    

    【讨论】:

    • } else if (matchString.length > 2 && matchString[2].toUpperCase().contains("VK-iPaytm") || matchString[2].toUpperCase().contains("AD -IPAYTM")) 我试过这个仍然得到同样的异常
    • 什么异常?
    • java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 这个
    • 我认为问题不在于其他如果...最好删除该行并检查其是否正常工作..
    • @sasikumar 我相信它应该像if (matchString.length > 2 && ( matchString[2].toUpperCase().contains("VK-iPaytm") || matchString[2].toUpperCase().contains("AD-IPAYTM"))) wrap or condition inside "()'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    • 2015-06-16
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    相关资源
    最近更新 更多