gpt4 book ai didi

尝试迭代字符串时出现 java.lang.StringIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-11-30 01:57:20 25 4
gpt4 key购买 nike

我正在尝试编写一个应用程序来接受用户输入并将其转换为莫尔斯电码

函数代码:

    public static String[] stringToMorseSequence(String stringOfLetters){
int lenStringOfLetters = stringOfLetters.length();
String output[] = new String[1];

String compInterpreted = "";
for(int i = 0; i < lenStringOfLetters; i++) {
String focus = String.valueOf(stringOfLetters.charAt(i));
String nextCharacter = "";
if (i != lenStringOfLetters) {
nextCharacter = String.valueOf(stringOfLetters.charAt(i + 1));
} else {
nextCharacter = "END";
}

if (focus == " ") {
compInterpreted = compInterpreted + "#";
} else {
String morseSequence = processCharacter(focus);
if (morseSequence == "") {

} else if (i == lenStringOfLetters) {
compInterpreted = compInterpreted + "!";
} else if (nextCharacter != "END") {
if (nextCharacter != " "){
compInterpreted = compInterpreted + morseSequence + "@";
} else {
compInterpreted = compInterpreted + morseSequence;
}
}
}
}
Log.i("MORSE","Computer interpretable morse sequence = " + compInterpreted);
output[0] = compInterpreted;

String prettySequence = "";
for(int i = 0; i-1 < output[0].length(); i++) {
switch(String.valueOf(output[0].charAt(i))){
case("#"):
prettySequence = prettySequence + " ";
case("@"):
prettySequence = prettySequence + " ";
default:
prettySequence = prettySequence + output[0].charAt(i);
}
}
Log.i("MORSE","Human interpretable morse sequence = " + prettySequence);
output[1] = prettySequence;

return output;
}

我似乎收到了这个异常

E/AndroidRuntime: FATAL EXCEPTION: main
Process: me.merhlim.jessica.morsecode, PID: 5687
java.lang.StringIndexOutOfBoundsException: length=10; index=10
at java.lang.String.charAt(Native Method)
at me.merhlim.jessica.morsecode.MorseProcessing.stringToMorseSequence(MorseProcessing.java:16)
at me.merhlim.jessica.morsecode.texttomorse$7.onClick(texttomorse.java:112)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:108)

我知道这是一个逻辑错误,但我不确定这个错误在哪里,或者如何解决它。正如错误消息中提到的,当尝试解析字符串中的特定字符时,它是一个索引错误,但我不确定触发此错误的逻辑是什么

我是java新手,而且已经是深夜了,所以也许我没有完全理解我正在编写的逻辑中的问题

如果这还不足以解决我的问题,我可以提供更多信息

感谢您花时间阅读本文,如果您提供帮助,我们将不胜感激-杰西卡

最佳答案

当 i 为 9 且长度小于 10 时,调用

nextCharacter = String.valueOf(stringOfLetters.charAt(i + 1));

将尝试获取第 10 个位置的字符。然而,字符串的长度只有10,最后一个字符位于位置9,这会导致字符串索引越界异常。你可能想改变这个

if (i != lenStringOfLetters) {

if (i != lenStringOfLetters-1) {

关于尝试迭代字符串时出现 java.lang.StringIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53952521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com