[英]String index out of range: -1
//filename 1.xml
<category
hello world
</category>
//when iam trying to parse this file using the following code it throws String index out of range: -1
output: startPos: -1
endPosi: -1
String dataLine = nextLine.trim();
int startPos = dataLine.indexOf(startToken);
logger.debug("startPos: " + startPos);
int endPosi = dataLine.lastIndexOf(endToken);
logger.debug("endPosi: " + endPosi);
// 2.xml it parses this file which contains the following line
<category hello world </category>
//這兩個文件的唯一區別是第一個文件的內容為三行,第二個文件的內容為一行。
將輸入文件更改為:
<category>
hello world
</category>
然后您的起始令牌為:
String startToken = "<category>";
您遇到的一個問題是<category
是無效的XML。 .trim()
在第一個文件的第一行上.trim()
尾隨空格。 簡短答案:修復您的XML。
int startPos = dataLine.indexOf(startToken);
我認為您的dataLine字符串中沒有startToken,因此它返回-1;
如果字符串參數作為該對象內的子字符串出現,則返回第一個此類子字符串的第一個字符的索引; 如果它不是作為子字符串出現,則返回-1。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.