gpt4 book ai didi

java - String.charAt 的 StringIndexOutOfBoundsException 错误

转载 作者:行者123 更新时间:2023-12-01 12:30:21 25 4
gpt4 key购买 nike

我创建了一个关于从网页获取元素的 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());

请帮我解决发生的错误。

最佳答案

该错误意味着您试图从索引大于或等于字符串长度的字符串中获取字符。具体来说,当您尝试获取第 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;
}
}

关于java - String.charAt 的 StringIndexOutOfBoundsException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25966036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com