[英]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.