我正在制作一个聊天机器人,它需要将每个句子分解为字符串数组列表。每当我使用空格时,它都会给我一个字符串索引越界异常。老实说,我不知道该怎么办,我查遍了论坛,请帮忙。
char tempChar;
String tempLetter;
String tempString = "";
for (int i = 1; i <= input.length(); i++) {
Scanner breakDownScan = new Scanner(input);
tempChar = breakDownScan.next().charAt(i-1);
if (tempChar != ' ' && tempChar != '.' && tempChar != '!' && tempChar != '?') {
tempLetter = Character.toString(tempChar);
tempString += tempLetter;
}
if (tempChar == ' ' || tempChar == '.' || tempChar == '!' || tempChar == '?') {
System.out.println("test");
words.add(tempString);
}
if (i == input.length()) {
breakDownScan.close();
}
}
提前感谢您提供的所有帮助:D
最佳答案
只需使用 split 方法即可。示例:
String[] words = input.split(insert your desired parser here);
我相信你可以用数组列表做同样的事情:) (对于解析器,使用空格(“”)或任何单词分隔符)
关于java - 使用空格时字符串索引越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31999521/