【发布时间】:2018-11-26 01:45:08
【问题描述】:
我的字符串有超过 100000 个单词。它是一本书。它包含大约 x 个章节:
chapter 1
text text text
chapter 2
text text text
and so on
如何获得总章数?(最后一章数)?
例如:chapter 117
我试过这个:
String[] words = book.split(" ");
ArrayList<Integer> chapterPositions = new ArrayList<Integer>();
int count = 0;
for (String a : words) {
if (a.equals("Chapter")) {
chapterPositions.add(count + 1);
}
count++;
}
num_chapters = Integer.parseInt(words[(chapterPositions.get(chapterPositions.size() - 1))]);
Toast.makeText(getApplicationContext(), Integer.toString(num_chapters), Toast.LENGTH_LONG).show();
但得到异常:NumberFormatException: For input string: "1 The"
【问题讨论】:
-
这可以在 Linux 中轻松完成,但您在这里使用 Java 吗?
-
是的,先生。绝对