Java 我一直遇到一条错误消息,说字符串索引超出范围。如何修复?

Java 我一直遇到一条错误消息,说字符串索引超出范围。如何修复?,java,loops,for-loop,Java,Loops,For Loop,我不断遇到错误,如何修复?这意味着什么?为什么?我很困惑。他一再失败。如何避免超出字符串长度?我再也不知道了 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 35 at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47) at java.base

我不断遇到错误,如何修复?这意味着什么?为什么?我很困惑。他一再失败。如何避免超出字符串长度?我再也不知道了

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 35
    at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
    at java.base/java.lang.String.charAt(String.java:693)
    at Mars.main(Mars.java:43)
这是我的密码:

import java.lang.Math;
import java.util.Scanner;
import java.io.*;

public class Mars {
    /* decides whether a martian message contains a swear or not */
    public static void main(String args[]) {
      Scanner scan = new Scanner (System.in);
      String m;
      String u;
      System.out.println("Please enter Martian username.");
      u = scan.nextLine();
      System.out.println("Please enter Martian message.");
      m = scan.nextLine();
      /* asks for message and username */
      System.out.println(u + " - " + m);
      /* prints the message */
      String f = (u + " - " + m);
      String um = f.toLowerCase();
      int L = um.length();
      /* loop variable */
      int V = 0;
      int qey = 0;
      int s$s = 0;
      /* swear variables */
      for (int c = 0; c <= L;){
          System.out.println(um.charAt(c));
          if (c > (L - 2)){
              
          }
          else if (um.charAt(c) == ' ' & um.charAt((c + 1)) == '^' & um.charAt((c + 2)) == '^' & um.charAt((c + 3)) == ' ' || c == 0 && um.charAt(c) == '^' & um.charAt((c + 1)) == '^' & um.charAt((c + 2)) == ' ' || c == (L - 2) & um.charAt(c) == ' ' & um.charAt(c + 1) == '^' & um.charAt((c + 2)) == '^'){
              V ++;
          }
          if (c > (L - 1)){
              
          }
          else if (um.charAt(c) == ' ' & um.charAt((c + 1)) == '$' & um.charAt((c + 2)) == ' ' || c == 0 & um.charAt(c) == '$' & um.charAt((c + 1)) == ' ' || c == (L - 1) & um.charAt(c) == ' ' & um.charAt(c + 1) == '$'){
              s$s ++;
          }
          if (c > (L - 3)){
              
          }
          else if (um.charAt(c) == ' ' & um.charAt((c + 1)) == 'q' & um.charAt((c + 2)) == 'e' & um.charAt((c + 3)) == 'y' & um.charAt((c + 4)) == ' ' || c == 0 && um.charAt(c) == 'q' & um.charAt((c + 1)) == 'e' & um.charAt((c + 2)) == 'y' & um.charAt((c + 3)) == ' ' || c == (L - 4) & um.charAt(c) == ' ' & um.charAt(c + 1) == 'q' & um.charAt((c + 2)) == 'e' & um.charAt(c + 3) == 'y'){
              qey ++;
          }
          c++;
      }
      /* checks if there are any swears and adds 1 to the variable assigned to that swear */
      System.out.println("Results:");
      if (qey >= 1 || s$s >= 1 || V >= 1){
          System.out.println("BAD" + "\n" + u + "\n" + "^^: " + V + "\n" + "qey: " + qey + "\n" + "$: " + s$s);
      }
      else{
          System.out.println("CLEAN");
      }
      /* checks for any swears */
    }
}
import java.lang.Math;
导入java.util.Scanner;
导入java.io.*;
公共级火星{
/*决定火星信息是否包含誓言*/
公共静态void main(字符串参数[]){
扫描仪扫描=新扫描仪(System.in);
字符串m;
u字串;
System.out.println(“请输入火星用户名”);
u=scan.nextLine();
System.out.println(“请输入火星消息”);
m=scan.nextLine();
/*询问消息和用户名*/
系统输出打印项次(u+“-”+m);
/*打印消息*/
字符串f=(u+“-”+m);
字符串um=f.toLowerCase();
int L=um.length();
/*循环变量*/
int V=0;
int qey=0;
整数s$s=0;
/*发誓变量*/
对于(int c=0;c(L-2)){
}
否则,如果‘‘‘‘‘‘’{
V++;
}
如果(c>(L-1)){
}
否则,如果(um.charAt(c)=''.'和um.charAt((c+1))=''.'和um.charAt((c+2))='.'.| c==0和um.charAt(c)='.'和um.charAt((c+1))='.| c==(L-1)和um.charAt(c)='.&um.charAt(c+1)='.'{
s$s++;
}
如果(c>(L-3)){
}
“q”和“um.charAt”(c+2)的意思((c+2+c+2))==“q”和“um.charAt”(c+2))=“e”和“um.charAt”(c+3)(c+3))=“y”和“y”和“um.charAt”(c+3)(c+c+3))==“y”和“y”和“y”和“um.charAt(c.charAt(c+3)(c+3)(c+4)(c+4))=========““y”和“y”和“y”和“um.CharT(c+3(c+3)和(c+3)(c+3)(c+3)(c+3)三)三)3)3)3)三)三)三)三)三)三)三(3)三(3)三(3)三)三)三(3)三)三(3)三(3)三)三)三)三)三字符((c+2))=='e'和um.charAt(c+3)='y'){
qey++;
}
C++;
}
/*检查是否存在任何宣誓,并将1添加到指定给该宣誓的变量*/
System.out.println(“结果:”);
如果(qey>=1 | s$s>=1 | V>=1){
System.out.println(“坏“+”\n“+u+”\n“+”^:“+V+”\n“+”qey:“+qey+”\n“+”$:“+s$s”);
}
否则{
系统输出打印项次(“清洁”);
}
/*检查是否有人发誓*/
}
}

如果字符串长度为N,则其字符编号为0、1、。。。N-1

没有字符(N)。该索引不在字符串的末尾

因此,任何循环都必须从0运行到N-1,而不是从0运行到N


也就是说,
for(c=0;c
和not
for(c=0;c通过在末尾添加一些空格并使for循环不会一直运行来修复它

      String f = (u + " - " + m + "                          ");
      String um = f.toLowerCase();
      int L = um.length();
      /* loop variable */
      int V = 0;
      int qey = 0;
      int s$s = 0;
      /* swear variables */
      for (int c = 0; c <= (L - 5);){
         if (um.charAt(c) == ' ' & um.charAt((c + 1)) == '$' & um.charAt((c + 2)) == ' ' || c == 0 & um.charAt(c) == '$' & um.charAt((c + 1)) == ' '){
              s$s ++;
         }
         if (um.charAt(c) == ' ' & um.charAt((c + 1)) == '^' & um.charAt((c + 2)) == '^' & um.charAt((c + 3)) == ' ' || c == 0 && um.charAt(c) == '^' & um.charAt((c + 1)) == '^' & um.charAt((c + 2)) == ' '){
              V ++;
         }
         else if (um.charAt(c) == ' ' & um.charAt((c + 1)) == 'q' & um.charAt((c + 2)) == 'e' & um.charAt((c + 3)) == 'y' & um.charAt((c + 4)) == ' ' || c == 0 && um.charAt(c) == 'q' & um.charAt((c + 1)) == 'e' & um.charAt((c + 2)) == 'y' & um.charAt((c + 3)) == ' '){
              qey ++;
         }
         c++;
      }
字符串f=(u+“-”+m+”);
字符串um=f.toLowerCase();
int L=um.length();
/*循环变量*/
int V=0;
int qey=0;
整数s$s=0;
/*发誓变量*/

对于(int c=0;c)如果你能发布你的代码片段,正确回复会很有帮助。现在就这么做。网站对那个愚蠢的字码比很恼火。@IanLebowitz下次,尝试使用块代码(缩进4个空格)而不是内联代码。L等于um的长度。循环条件是
int c=0;c我在末尾添加它,因为如果它在开头添加,它将忽略第一个字母。这不起作用什么“不起作用”?无可争议的事实是,字符的索引为0到N-1。因此,你的工作是发现代码的索引位置>=N。仔细查看代码,我至少看到了4个地方会发生这种情况。这是一个相当奇怪的固定定义。它对我有效。这可能是-但对你的讲师可能不起作用。
      if (c > (L - 2)){
          
      }
      else if (um.charAt(c) == ' ' & um.charAt((c + 1)) == '^' & um.charAt((c + 2)) == '^' ....
      String f = (u + " - " + m + "                          ");
      String um = f.toLowerCase();
      int L = um.length();
      /* loop variable */
      int V = 0;
      int qey = 0;
      int s$s = 0;
      /* swear variables */
      for (int c = 0; c <= (L - 5);){
         if (um.charAt(c) == ' ' & um.charAt((c + 1)) == '$' & um.charAt((c + 2)) == ' ' || c == 0 & um.charAt(c) == '$' & um.charAt((c + 1)) == ' '){
              s$s ++;
         }
         if (um.charAt(c) == ' ' & um.charAt((c + 1)) == '^' & um.charAt((c + 2)) == '^' & um.charAt((c + 3)) == ' ' || c == 0 && um.charAt(c) == '^' & um.charAt((c + 1)) == '^' & um.charAt((c + 2)) == ' '){
              V ++;
         }
         else if (um.charAt(c) == ' ' & um.charAt((c + 1)) == 'q' & um.charAt((c + 2)) == 'e' & um.charAt((c + 3)) == 'y' & um.charAt((c + 4)) == ' ' || c == 0 && um.charAt(c) == 'q' & um.charAt((c + 1)) == 'e' & um.charAt((c + 2)) == 'y' & um.charAt((c + 3)) == ' '){
              qey ++;
         }
         c++;
      }