有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java。lang.StringIndexOutOfBoundsException:字符串索引超出范围:11

我被这件事缠住了,找不到解决办法。获取线程“main”java中的异常错误。lang.StringIndexOutOfBoundsException:字符串索引超出范围:11

有人能帮忙解决这个问题吗? 我的代码:

public static void main(String[] args) {
    try {
        Scanner sc = new Scanner(new File("Testing.txt"));
        int i = 0;

        while(sc.hasNext()){
            String line = sc.nextLine();
            char needle = line.charAt(i);

            while(i < line.length()){
                if(Character.isUpperCase(needle)) {
                    while(needle != ' '){
                        System.out.print(needle);
                        i++;
                        needle = line.charAt(i);
                    }
                    System.out.println(needle);
                }
                else{
                    i++;
                    needle = line.charAt(i);
               }
            }
        }
    } 
    catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
    }
}

共 (3) 个答案

  1. # 1 楼答案

    根据我上面的评论,使用Stringsplit方法是很容易的。当然input可以替换为文件中的文本,使用expectedResult和测试输出是否等于完全是可选的

      public static void main(String[] args)
      {
    
          String input = "Kenny and I are eating in the Restaurant Yummy";
          String expectedResult = "Kenny, I, Restaurant, Yummy";
          String arr [] = input.split (" ");
    
          StringBuilder out = new StringBuilder();
          for (int i = 0; i < arr.length; i++) {
              if (Character.isUpperCase(arr[i].charAt(0))) {
                  out.append(arr[i]).append(", ");
              }
          }
    
          if (out.length() > 1) {
              out.setLength(out.length() -2);
          }
    
          System.out.println (out);
          assert (expectedResult.equals(out.toString()));
    
      }  
    

    输出

    Kenny, I, Restaurant, Yummy

  2. # 2 楼答案

    我的假设是你的错误就在这里

                    i++;
                    needle = line.charAt(i);
    

                    while(needle != ' '){
                        System.out.print(needle);
                        i++;
                        needle = line.charAt(i);
                    }
    

    在增加索引时,不检查索引是否存在

  3. # 3 楼答案

    我不确定你想要实现什么。但是从您的错误中,我可以说您正在尝试递增索引计数器,然后尝试访问字符串中的元素

    while循环和else块中,有以下语句:

    i++;
    needle = line.charAt(i);
    

    将两者都更改为:

    needle = line.charAt(i);
    i++;
    

    您的代码将在没有上述异常的情况下运行

    Update

    我做了上面提到的更改,并做了一些更改

    1. 在第一个while循环中包含int i=0;的声明。文本文件中不止一行需要这样做
    2. 在第二个while循环中包含声明char needle = line.charAt(i);语句,因为需要为每一行初始化指针

    try (Scanner sc = new Scanner(new File("Testing.txt"))) {
    
        while (sc.hasNext()) {
            String line = sc.nextLine();
            int i = 0;
    
            while (i < line.length()) {
                char needle = line.charAt(i);
                if (Character.isUpperCase(needle)) {
                    while (needle != ' ' && i < line.length()) {
                        needle = line.charAt(i);
                        if(needle != ' '){
                            System.out.print(needle);
                        }
                        i++;
                    }
                    if(i < line.length()) {
                        System.out.print(", ");
                    }
                } else {
                    needle = line.charAt(i);
                    i++;
                }
            }
            System.out.println();
        }
    } catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
    }
    

    它打印以下输出:

    Kenny, I, Restaurant, Yummy
    

    希望这有帮助