简体   繁体   English

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

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

The problem in question is indentifying if a string has a comma and outputting the substrings from the original string.有问题的问题是识别字符串是否有逗号并从原始字符串中输出子字符串。

Here is 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;
   }
   
   
}

The error I keep receiving when I compile is this:我编译时不断收到的错误是:

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)

I'm still somewhat new to programming and have never seen this type of error before, what are some ways to solve this and what might be causing it?我对编程还有些陌生,以前从未见过这种类型的错误,有什么方法可以解决这个问题以及可能导致它的原因是什么?

The exception occurs when the index is out of range.当索引超出范围时会发生异常。 What is a StringIndexOutOfBoundsException? 什么是 StringIndexOutOfBoundsException? How can I fix it? 我该如何解决?

For your code you are not re intializing the value of variable checkForComma对于您的代码,您没有重新初始化变量 checkForComma 的值

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

If checkForComma=-1, it will then take the next input and jump to如果 checkForComma=-1,它将接受下一个输入并跳转到

 firstSubstring = fullString.substring(0, checkForComma);

A string index cannot be -1/negative, so it shows error.字符串索引不能为 -1/负数,因此会显示错误。

Solution for the error You should reinatialize the value of checkForComma, as per your program intake but dont let it over pass the range of variable fullString .错误的解决方案您应该根据您的程序摄入量重新初始化 checkForComma 的值,但不要让它超过变量fullString的范围。

Instead of using continue in the else when u check if the checkForComma variable equals -1, u can directly use the all other code which should be running when checkForComma has a real value.当您检查 checkForComma 变量是否等于 -1 时,您可以直接使用当 checkForComma 具有实际值时应该运行的所有其他代码,而不是在 else 中使用continue

Just replace this part of the code.只需替换这部分代码。

            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();
            }

And to get the second word, you can use only the beginning input which is checkForComma + 1 in this case as this will return the value till the end of the string.要获得第二个单词,在这种情况下,您只能使用checkForComma + 1的开头输入,因为这将返回直到字符串末尾的值。

暂无
暂无

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

相关问题 线程“main”中的异常 java.lang.StringIndexOutOfBoundsException - Exception in thread "main" java.lang.StringIndexOutOfBoundsException 线程主java.lang.StringIndexOutOfBoundsException中的异常 - exception in thread main java.lang.StringIndexOutOfBoundsException 为什么我不断收到:线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:4 - Why do I keep geting: Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 4 为什么程序会抛出这个错误??:线程“main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:36 - Why is the program throwing this error??: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 36 我不断收到此错误:线程“main”中的异常 java.lang.StringIndexOutOfBoundsException: String index out of range: 28 - I keep getting this error : Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 28 我收到如下错误消息:线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 - I get an error message as follows: Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 0 在 Java 中获取“线程“main”java.lang.StringIndexOutOfBoundsException 中的异常” - Getting "Exception in thread "main" java.lang.StringIndexOutOfBoundsException" in Java 我的程序出现运行时错误:线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:27 - My program has a runtime error: exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 27 编译Java项目后出错:线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 - Error after compiling Java project : Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 0 该程序不断给我一个错误:线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:4 - This program keeps on giving me an error: Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM