繁体   English   中英

为什么我在代码中的线程“main”java.lang.StringIndexOutOfBoundsException 错误中收到异常?

[英]Why am I receiving an Exception in thread "main" java.lang.StringIndexOutOfBoundsException error in my code?

有问题的问题是识别字符串是否有逗号并从原始字符串中输出子字符串。

这是我的代码:

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

public class ParseStrings {
   public static void main(String[] args) {
      
      Scanner scnr = new Scanner(System.in);
      String fullString = "";
      int checkForComma = 0;
      String firstSubstring = "";
      String secondSubstring = "";
      boolean checkForInput = false;
      
      while (!checkForInput) {
         System.out.println("Enter input string: ");
         fullString = scnr.nextLine();
         
         if (fullString.equals("q")) {
            checkForInput = true;
         }
         else {
            checkForComma = fullString.indexOf(',');
            
            if (checkForComma == -1) {
               System.out.println("Error: No comma in string");
               fullString = scnr.nextLine();
            }
            
            else {
               continue;
            }
            
            firstSubstring = fullString.substring(0, checkForComma);
            secondSubstring = fullString.substring(checkForComma + 1, fullString.length());
            
            System.out.println("First word: " + firstSubstring);
            System.out.println("Second word: " + secondSubstring);
            System.out.println();
            System.out.println();
         }
         
         
      }
      
      return;
   }
   
   
}

我编译时不断收到的错误是:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 10
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319)
at java.base/java.lang.String.substring(String.java:1874)
at ParseStrings.main(ParseStrings.java:34)

我对编程还有些陌生,以前从未见过这种类型的错误,有什么方法可以解决这个问题以及可能导致它的原因是什么?

当索引超出范围时会发生异常。 什么是 StringIndexOutOfBoundsException? 我该如何解决?

对于您的代码,您没有重新初始化变量 checkForComma 的值

if (checkForComma == -1) 
       {
           System.out.println("Error: No comma in string");
           fullString = scnr.nextLine();
        }

如果 checkForComma=-1,它将接受下一个输入并跳转到

 firstSubstring = fullString.substring(0, checkForComma);

字符串索引不能为 -1/负数,因此会显示错误。

错误的解决方案您应该根据您的程序摄入量重新初始化 checkForComma 的值,但不要让它超过变量fullString的范围。

当您检查 checkForComma 变量是否等于 -1 时,您可以直接使用当 checkForComma 具有实际值时应该运行的所有其他代码,而不是在 else 中使用continue

只需替换这部分代码。

            checkForComma = fullString.indexOf(',');
            
            if (checkForComma == -1) {
               System.out.println("Error: No comma in string");
               fullString = scnr.nextLine();
            }
            
            else {
               firstSubstring = fullString.substring(0, checkForComma);
               secondSubstring = fullString.substring(checkForComma + 1);
            
               System.out.println("First word: " + firstSubstring);
               System.out.println("Second word: " + secondSubstring);
               System.out.println();
               System.out.println();
            }

要获得第二个单词,在这种情况下,您只能使用checkForComma + 1的开头输入,因为这将返回直到字符串末尾的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM