gpt4 book ai didi

java - 如何修复字符串索引超出范围 : -1

转载 作者:行者123 更新时间:2023-11-29 04:06:08 27 4
gpt4 key购买 nike

我一直在研究一种生成密码的方法,该方法会将每个“S”更改为 $。

请注意,我从另一个类中获取短语,它总是大于 8 个字符

    String key;
String store;
key = phrase.substring(0,1).toUpperCase();
phrase = key + phrase.substring(1,phrase.length());
System.out.println(phrase);
System.out.println(phrase.length());
for(int i = phrase.length(); i>0; i--) {
int sKey = phrase.indexOf('S');
store = "$" + phrase.substring(sKey+1,phrase.length());
phrase =phrase.substring(0,sKey)+store;
System.out.print(phrase);
}
}

但是之后总是会出现这个错误

Exception in thread "main" Te$taaaajava.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(Unknown Source)
at edu.ilstu.Security.generatePassword(Security.java:15)
at edu.ilstu.SecurityApp.main(SecurityApp.java:57)

最佳答案

索引超出范围异常值 -1 表示未找到请求的符号,在本例中为 S。

关于java - 如何修复字符串索引超出范围 : -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58550223/

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