【问题标题】:StringIndexOutOfBoundsException error for String.charAt [duplicate]String.charAt 的 StringIndexOutOfBoundsException 错误 [重复]
【发布时间】:2014-09-22 02:47:06
【问题描述】:

我创建了一个关于从网页获取元素的 java 应用程序,这是代码。

public class rpms {

    public rpms() {

    }

    private static final String USER_AGENT = "Mozilla/5.0";
    private static final String URL ="https://url.com";

    public static void main(String[] args) {
        URLget labaccess = new URLget();

        try {
            getElementTd(sendGetRequest(URL).toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String sendGetRequest(String urlString) throws IOException {

        URL obj = new URL(urlString);
        HttpURLConnection httpConnection = (HttpURLConnection) obj
                .openConnection();

        httpConnection.setRequestMethod("GET");

        httpConnection.setRequestProperty("User-Agent", USER_AGENT);

        int responseCode = httpConnection.getResponseCode();
        if (responseCode == 200) {

            BufferedReader responseReader = new BufferedReader(
                    new InputStreamReader(httpConnection.getInputStream()));

            String responseLine;
            StringBuffer response = new StringBuffer();

            while ((responseLine = responseReader.readLine()) != null) {
                response.append(responseLine + "\n");
            }
            responseReader.close();

            // print result
            return response.toString();
        }
        return null;
    }

    public static void getElementTd(String sourceCode)
            throws FileNotFoundException, UnsupportedEncodingException {
        String fragment = sourceCode;
        ArrayList<String> ip = new ArrayList<String>();
        Document doc = Jsoup.parseBodyFragment(fragment);

        Elements elements = doc.select("td p");

        for (int i = 0; i < elements.size(); i++) {
            // System.out.println(i+1+" ) "+elements.eq(i).text().toString());

            if (fileExplode(elements.eq(i).text().toString())) {
                System.out.println(elements.eq(i).text().toString());

            }
        }
    }

    public static boolean fileExplode(String str1) {
        boolean hasRPM = false;
        String[] split1 = str1.replace(".", " ").split(" ");

        for (int i = 0; i < split1.length; i++) {
            if ((i + 1) == split1.length) {
                if (split1[i].equalsIgnoreCase("rpm")
                        || (split1[i].charAt(0) == 'r'
                                && split1[i].charAt(1) == 'p' && split1[i]
                                .charAt(2) == 'm')) {
                    hasRPM = true;

                }break;
            }

        }
        return hasRPM;
    }   
}

但是当我执行上面的代码时。发生错误:线程“main”中的异常我不知道这是什么意思。 charAt 出错。

 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
 at java.lang.String.charAt(Unknown Source)
 at rpms.fileExplode(rpms.java:98)
 at rpms.getElementTd(rpms.java:82)
 at rpms.main(rpms.java:32)

为了获得更多指导,这是发生错误的数字。

/*97*/ if (split1[split1.length - 1].equalsIgnoreCase("rpm")
/*98*/|| (split1[i].charAt(0) == 'r'
/*99*/ && split1[i].charAt(1) == 'p' && split1[i]
/*100*/ .charAt(2) == 'm')) {
/*82*/ if (fileExplode(elements.eq(i).text().toString())) {
/*83*/ System.out.println(elements.eq(i).text().toString());
/*84*/ }
/*32*/ getElementTd(sendGetRequest(URL).toString());

请帮我解决发生的错误。

【问题讨论】:

  • 您正在尝试获取零字符长的字符串的第一个字符。

标签: java eclipse jsoup httpconnection


【解决方案1】:

这个错误意味着你试图从一个大于或等于字符串长度的索引处的字符串中获取一个字符。具体来说,是当您尝试在第 98 行获取 split[i] 中的第一个字符时。您永远不会检查 split1[i] 的大小是否大于 0。我建议添加它。

for (int i = 0; i < split1.length; i++) {
    if ((i + 1) == split1.length) {
        if (split1[i].equalsIgnoreCase("rpm")
                || (split1[i].length > 2 && split1[i].charAt(0) == 'r'
                        && split1[i].charAt(1) == 'p' && split1[i]
                        .charAt(2) == 'm')) {
            hasRPM = true;
        }

        break;
    }
}

【讨论】:

  • @chrisHayes 谢谢你的回答,错误现在消失了。解决了
  • @blessJator 这实际上是医生的回答,我只是做了一个小编辑。如果答案对您有帮助,请务必接受(点击答案旁边的复选框)。
  • @ChrisHayes 哈哈哈谢谢克里斯
猜你喜欢
  • 2016-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多