웹 페이지에서 요소를 가져 오는 것에 대한 Java 응용 프로그램을 만들었습니다. 이것은 코드입니다.String.charAt에 대한 StringIndexOutOfBoundsException 오류
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)
자세한 안내는 오류가 발생한 번호입니다.
/*32*/ getElementTd(sendGetRequest(URL).toString());
/*82*/ if (fileExplode(elements.eq(i).text().toString())) {
/*83*/ System.out.println(elements.eq(i).text().toString());
/*84*/ }
/*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')) {
나 오류가 발생한 해결하는 데 도움이 바랍니다.
길이가 0자인 문자열의 첫 번째 문자를 가져 오려고합니다. –