输入空字符串时出现 StringIndexOutofBoundsException 错误 Java
StringIndexOutofBoundsException Error when inputing an empty String Java
嗨,每当我尝试输入空字符串时,我总是收到此错误。到目前为止,其他一切都有效,如果我在 String 中放置一个 space 它就有效。我知道这真的很挑剔,但我很好奇在这种情况下我应该怎么做才能确保它 returns 只是一个空字符串。
**> HW2.nthWord(2,"")
java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at HW2.nthWord(HW2.java:124)**
我确实为输入这个值创建了一个特殊实例,但它仍然不起作用。
我需要做什么来纠正这个问题?
/*nthWord takes an int and a String as input and returns a String:
The input int represents a number n that is assumed to be positive, and the output string
contains every nth word of the input string, starting with the first word, separated by a single space.
For this method, a word is defined to be a sequence of non-space characters.
There should be no space at the end of the output string.
*/
public static String nthWord( int number, String input ){
StringBuilder create = new StringBuilder();
int totalspaces = 0; //This is to hold a count of the number of spaces in a String
if( number == 0){
return input;
}
if(input == ""){
return input;
}
else{
for(int i = 0; input.charAt(i) != ' '; i = i + 1){
create.append(input.charAt(i));
}
for( int i = 0; i < input.length() - 1 ; i = i + 1){
if(input.charAt(i) == ' ' && i < input.length() - 1 && input.charAt(i+1) != ' '){
if( i != input.length()-1 && input.charAt(i+1) != ' '){
totalspaces = totalspaces + 1;
}
if(totalspaces % number == 0 && totalspaces != 0){
create.append(' ');
for(int j = i+1; input.charAt(j) != ' ' && j < input.length(); j = j+1){
create.append(input.charAt(j));
i = j;
}
}
}
}
return create.toString();
}
}
我注意到一些事情
for(int i = 0; input.charAt(i) != ' '; i = i + 1){
create.append(input.charAt(i));
}
此循环将不断添加 "input" 个字符,直到达到 space' ' 个字符。如果输入没有 space 字符,那么此循环将超出输入的长度并导致错误。你可能想要这样的东西:
for(int i = 0; i < input.length(); i = i + 1){
if(input.charAt(i) == ' ' ){
break;
} else {
create.append(input.charAt(i));
}
}
此外,当您到达该行时:
if(input.charAt(i) == ' ' && i < input.length() - 1 && input.charAt(i+1) != ' '){
您已经知道 i < input.length() - 1 因为您处于 for 循环中。您可以将该行更改为:
if(input.charAt(i) == ' ' && input.charAt(i+1) != ' '){
出于同样的原因,你的下一节:
if( i != input.length()-1 && input.charAt(i+1) != ' '){
totalspaces = totalspaces + 1;
}
可以改成
if( i != input.length()-1 ){
totalspaces = totalspaces + 1;
}
此外,我注意到您可能使问题变得比需要的更难。如果您在单个 for 循环中解决问题,问题会容易得多。
for(int i = 0; i < input.length(); i = i + 1){
if( x ) //x is some code that determines if you are part of the nth word
create.append(input.charAt(i));
}
嗨,每当我尝试输入空字符串时,我总是收到此错误。到目前为止,其他一切都有效,如果我在 String 中放置一个 space 它就有效。我知道这真的很挑剔,但我很好奇在这种情况下我应该怎么做才能确保它 returns 只是一个空字符串。
**> HW2.nthWord(2,"")
java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at HW2.nthWord(HW2.java:124)**
我确实为输入这个值创建了一个特殊实例,但它仍然不起作用。
我需要做什么来纠正这个问题?
/*nthWord takes an int and a String as input and returns a String:
The input int represents a number n that is assumed to be positive, and the output string
contains every nth word of the input string, starting with the first word, separated by a single space.
For this method, a word is defined to be a sequence of non-space characters.
There should be no space at the end of the output string.
*/
public static String nthWord( int number, String input ){
StringBuilder create = new StringBuilder();
int totalspaces = 0; //This is to hold a count of the number of spaces in a String
if( number == 0){
return input;
}
if(input == ""){
return input;
}
else{
for(int i = 0; input.charAt(i) != ' '; i = i + 1){
create.append(input.charAt(i));
}
for( int i = 0; i < input.length() - 1 ; i = i + 1){
if(input.charAt(i) == ' ' && i < input.length() - 1 && input.charAt(i+1) != ' '){
if( i != input.length()-1 && input.charAt(i+1) != ' '){
totalspaces = totalspaces + 1;
}
if(totalspaces % number == 0 && totalspaces != 0){
create.append(' ');
for(int j = i+1; input.charAt(j) != ' ' && j < input.length(); j = j+1){
create.append(input.charAt(j));
i = j;
}
}
}
}
return create.toString();
}
}
我注意到一些事情
for(int i = 0; input.charAt(i) != ' '; i = i + 1){
create.append(input.charAt(i));
}
此循环将不断添加 "input" 个字符,直到达到 space' ' 个字符。如果输入没有 space 字符,那么此循环将超出输入的长度并导致错误。你可能想要这样的东西:
for(int i = 0; i < input.length(); i = i + 1){
if(input.charAt(i) == ' ' ){
break;
} else {
create.append(input.charAt(i));
}
}
此外,当您到达该行时:
if(input.charAt(i) == ' ' && i < input.length() - 1 && input.charAt(i+1) != ' '){
您已经知道 i < input.length() - 1 因为您处于 for 循环中。您可以将该行更改为:
if(input.charAt(i) == ' ' && input.charAt(i+1) != ' '){
出于同样的原因,你的下一节:
if( i != input.length()-1 && input.charAt(i+1) != ' '){
totalspaces = totalspaces + 1;
}
可以改成
if( i != input.length()-1 ){
totalspaces = totalspaces + 1;
}
此外,我注意到您可能使问题变得比需要的更难。如果您在单个 for 循环中解决问题,问题会容易得多。
for(int i = 0; i < input.length(); i = i + 1){
if( x ) //x is some code that determines if you are part of the nth word
create.append(input.charAt(i));
}