Java子字符串错误:字符串索引超出范围
我正在为学校做作业。我试图找出如何从用户给定的字符串中获取单个单词。在我的例子中,单词总是用空格隔开。所以我的代码计算有多少空格,然后生成子字符串。如果可以的话,请帮忙Java子字符串错误:字符串索引超出范围,java,Java,我正在为学校做作业。我试图找出如何从用户给定的字符串中获取单个单词。在我的例子中,单词总是用空格隔开。所以我的代码计算有多少空格,然后生成子字符串。如果可以的话,请帮忙 System.out.print("Please enter a sentence: "); String userSentence=IO.readString(); String testWord=""; int countSpaces=0;
System.out.print("Please enter a sentence: ");
String userSentence=IO.readString();
String testWord="";
int countSpaces=0;
for(int j=0; j<userSentence.length(); j++){
if((userSentence.charAt(j))==' '){
countSpaces++;
}
}
for(int i=0; i<userSentence.length(); i++){
if(countSpaces>0){
while(userSentence.charAt(i)==' '){
i++;
countSpaces--;
}
testWord=userSentence.substring(i, userSentence.indexOf(" "));
i=i+(testWord.length()-1);
}
if(countSpaces==0){
testWord=userSentence.substring(i);
i=userSentence.length();
}
System.out.print(testWord);
System.out.print(“请输入句子:”);
String UserSession=IO.readString();
字符串testWord=“”;
int countSpaces=0;
对于(intj=0;j,由于您没有提供完整的代码部分,所以我将指出您发布的内容中的一个问题
下面是一个yourfor
循环,其计数器为“i”
for(int i=0; i<userSentence.length(); i++)
{
// … Rarely (if ever) is “ i ” changed inside this loop!
i++; // <--- this will cause a problem
i=userSentence.length(); // <--- so will this
}
考虑到它使用“i”作为索引,这并不罕见。
我只是想一想。既然您没有提供完整的代码部分,我就指出您发布的内容有一个问题
下面是一个yourfor
循环,其计数器为“i”
for(int i=0; i<userSentence.length(); i++)
{
// … Rarely (if ever) is “ i ” changed inside this loop!
i++; // <--- this will cause a problem
i=userSentence.length(); // <--- so will this
}
考虑到它使用“i”作为索引,这并不罕见。
只是一个想法。如果我们不需要计算空格,下面的代码会更干净,但我假设这是您的限制,所以我会继续使用它。(编辑:JohnG很荣幸,有比更改I
更好的方法来实现这一点)
问题是,usersential.indexOf(“”
函数将始终返回“
的第一个找到的位置,并且由于您不断增加i
,但没有对usersential
进行任何更改,子字符串(i,usersential.indexOf(“”)
命令不再有意义
上述问题的解决方案是声明一个余数
字符串,该字符串跟踪在找到下一个测试词
后剩余的用户语句
部分
另一个警告是,indexOf()
将返回-1,如果没有找到“
,这意味着您已经到了最后一个字了。在这种情况下,testWord
的值将设置为余数
的末尾
这是我得到的。再一次,超级笨重,但我不想重写你得到的一切:
System.out.print("Please enter a sentence: ");
String userSentence=IO.readString();
String testWord="";
int countSpaces=0;
for(int j=0; j<userSentence.length(); j++){
if((userSentence.charAt(j))==' '){
countSpaces++;
}
}
for(int i=0; i<userSentence.length(); i++){
while(i<userSentence.length() && userSentence.charAt(i)==' '){
i++;
countSpaces--;
}
if(i<userSentence.length()){
String remainder = userSentence.substring(i);
if(countSpaces==0){
testWord=userSentence.substring(i);
i=userSentence.length();
} else {
remainder = userSentence.substring(i);
int endOfWordIndex = remainder.indexOf(" "); // set end of word to first occurrence of " "
if(endOfWordIndex == -1){ // if no " " found,
endOfWordIndex = remainder.length(); // set end of word to end of sentence.
}
testWord=remainder.substring(0, endOfWordIndex);
i=i+(testWord.length()-1);
}
System.out.println("word: '" + testWord + "'");
System.out.print(“请输入句子:”);
String UserSession=IO.readString();
字符串testWord=“”;
int countSpaces=0;
对于(int j=0;j如果我们不必计算空格,下面的代码会更干净,但我假设这是您的限制,所以我会继续使用它。(编辑:JohnG很荣幸,有比更改I
更好的方法来实现这一点)
问题是,usersential.indexOf(“”
函数将始终返回“
的第一个找到的位置,并且由于您不断增加i
,但没有对usersential
进行任何更改,子字符串(i,usersential.indexOf(“”)
命令不再有意义
上述问题的解决方案是声明一个余数
字符串,该字符串跟踪在找到下一个测试词
后剩余的用户语句
部分
另一个警告是,indexOf()
将返回-1,如果没有找到“
,这意味着您已经到了最后一个字了。在这种情况下,testWord
的值将设置为余数
的末尾
这是我得到的。再一次,超级笨重,但我不想重写你得到的一切:
System.out.print("Please enter a sentence: ");
String userSentence=IO.readString();
String testWord="";
int countSpaces=0;
for(int j=0; j<userSentence.length(); j++){
if((userSentence.charAt(j))==' '){
countSpaces++;
}
}
for(int i=0; i<userSentence.length(); i++){
while(i<userSentence.length() && userSentence.charAt(i)==' '){
i++;
countSpaces--;
}
if(i<userSentence.length()){
String remainder = userSentence.substring(i);
if(countSpaces==0){
testWord=userSentence.substring(i);
i=userSentence.length();
} else {
remainder = userSentence.substring(i);
int endOfWordIndex = remainder.indexOf(" "); // set end of word to first occurrence of " "
if(endOfWordIndex == -1){ // if no " " found,
endOfWordIndex = remainder.length(); // set end of word to end of sentence.
}
testWord=remainder.substring(0, endOfWordIndex);
i=i+(testWord.length()-1);
}
System.out.println("word: '" + testWord + "'");
System.out.print(“请输入句子:”);
String UserSession=IO.readString();
字符串testWord=“”;
int countSpaces=0;
对于(int j=0;j)您需要计算句子中有多少空格,然后删除它们?我需要计算给定短语或句子中有多少单词。空格是单词之间的区别。出现的问题是testWord=usersential.substring(i,usersential.indexOf(“”))的问题;好的,你可以使用split(“”
)来代替这些循环。如果你在字符串上使用这个方法,它会将字符串
分解成一个字符串
数组。你作为参数传递给split()
方法的字符串将是“断点”例如:“像老板一样测试拆分”。拆分(“”->[“测试”、“拆分”、“像”、“a”、“老板”]
我还不允许使用数组。它必须在一个循环中完成,只是现在还不确定我的代码出了什么问题。我在一张纸上检查了它。索引的数字应该是正确的。你需要计算句子中有多少空格,然后删除它们?我需要计算一个gi中有多少单词即使是短语或句子。和空格是单词之间的区别。出现的问题是testWord=usersential.substring(i,usersential.indexOf(“”);好的,你可以使用split(“”)来代替这些循环
。如果对字符串使用此方法,它会将字符串
分解为一个字符串数组
。作为参数传递给split()
方法的字符串
将是字符串的“断点”。例如:“像老板一样测试拆分”。拆分(“”)->[“测试”,“拆分”,“like”、“a”、“boss”]
我现在还不允许使用数组。它必须在一个循环中完成,只是现在还不确定我的代码有什么问题。我在一张纸上检查了它。索引的数字应该是正确的。