【问题标题】:StringIndexOutofBoundsException Error when inputing an empty String Java [duplicate]输入空字符串Java时出现StringIndexOutofBoundsException错误[重复]
【发布时间】:2019-03-08 22:40:49
【问题描述】:

您好,每当我尝试输入空字符串时,我都会收到此错误。到目前为止,其他一切都有效,如果我在 String 内放置一个空格,它就可以工作。我知道这真的很挑剔,但我很好奇在这种情况下我应该怎么做才能确保它只返回一个空字符串。

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

【问题讨论】:

  • input == "" 是您问题的开始。这是比较字符串的错误方法。

标签: java string loops stringbuilder


【解决方案1】:

我注意到了一些事情

for(int i = 0; input.charAt(i) != ' '; i = i + 1){
   create.append(input.charAt(i));
}

这个循环将不断添加“输入”字符,直到它到达一个空格''字符。如果输入没有空格字符,则此循环将超出输入的长度并导致错误。你可能想要这样的东西:

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

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

【讨论】:

    猜你喜欢
    • 2013-11-22
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    相关资源
    最近更新 更多