【问题标题】:Why am I receiving an Exception in thread "main" java.lang.StringIndexOutOfBoundsException error in my code?为什么我在代码中的线程“main”java.lang.StringIndexOutOfBoundsException 错误中收到异常?
【发布时间】:2022-01-10 19:01:54
【问题描述】:

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

这是我的代码:

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)

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

【问题讨论】:

  • 我怀疑 continue 是否是您要放入第二个 else 块中的内容。我怀疑后面的六行实际上应该在else,而不是continue
  • 另外,当您编译您的程序时,您没有看到该错误。您在运行程序时看到了它。

标签: java string validation parsing


【解决方案1】:

当索引超出范围时发生异常。 What is a StringIndexOutOfBoundsException? How can I fix it?

对于您的代码,您没有重新初始化变量 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的范围。

【讨论】:

    【解决方案2】:

    当您检查 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,因为这将返回直到字符串末尾的值。

    【讨论】:

      猜你喜欢
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-25
      相关资源
      最近更新 更多