簡體   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