简体   繁体   English

使用空格时的字符串索引超出范围异常

[英]String Index out of bounds exception when using blank space

I'm making a chatbot, and it needs to break down every sentence into an array list of strings. 我正在创建一个聊天机器人,它需要将每个句子分解为一个字符串数组列表。 It gives me a string index out of bounds exception whenever I use a space. 每当我使用空格时,它都会给我一个超出范围的字符串索引异常。 I honestly don't know what to do about this, I've looked all over the forums, please help. 老实说,我不知道该怎么办,在各个论坛上我都看过,请帮忙。

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();
    }
}

Thank you in advanced for any and all help you can provide :D 非常感谢您可以提供的所有帮助:D

Just use the split method. 只需使用split方法。 Example: 例:

String[] words = input.split(insert your desired parser here);

I do believe you can do just the same with an Array List :) (for the parser, use a space(" "), or what ever your words are separated by) 我相信您可以使用Array List来做同样的事情:)(对于解析器,请使用space(“”),或者用您的单词分隔开的内容)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM