java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33727742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
提问by Ian Pierre
I'm learning Java and I don't know what is wrong here. Why is this error happening? I don't see anything wrong and that was actually working until when I wrote the line "count = 0" before the second "for" loop.
我正在学习 Java,但我不知道这里出了什么问题。为什么会发生此错误?我没有看到任何错误,直到我在第二个“for”循环之前写下“count = 0”行之前,这实际上是有效的。
That is the error: java.lang.StringIndexOutOfBoundsException: String index out of range: 1
那是错误:java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:1
That's the line where the error is happening:
那是发生错误的那一行:
if(mots.get(j).startsWith(searchPhrase.substring(0,1))){
Here is the entire code:
这是整个代码:
import java.util.*;
public class Test {
public static void main(String[] args) {
List<String> mots = new ArrayList<>();
List<String> saida = new ArrayList<>();
mots.add("un");
mots.add("deux");
mots.add("trois");
String searchPhrase = "aaasdeuxctsundesle";
int count = 0;
int countAnula = 0;
int azul = 0;
String anula = "-"; //Tem que fazer cast para char depois
String frase = searchPhrase;
for (int i = 0; i < frase.length(); i++) {
count = 0;
for (int j = 0; j < mots.size(); j++) {
if (mots.get(j).startsWith(searchPhrase.substring(0,1))) {
for (int t = 0; t < mots.get(j).length(); t++) {
if (searchPhrase.charAt(t) == mots.get(j).charAt(t)) {
azul ++;
} else {
break;
}
}
if (azul == mots.get(j).length()) {
saida.add(mots.get(j));
searchPhrase = searchPhrase.substring(mots.get(j).length());
j = 0;
azul = 0;
} else {
searchPhrase = searchPhrase.substring(1);
saida.add(anula);
j = 0;
azul = 0;
}
} else {
count ++;
}
}
if (count == mots.size()) {
searchPhrase = searchPhrase.substring(1);
saida.add(anula);
count = 0;
}
}
for (int g = 0; g < saida.size(); g++) {
System.out.println(saida.get(g));
}
}
}
回答by Tsung-Ting Kuo
As @ray said, checking the exception message shows that the problem occurs while searchPhrase is an empty string. In this case, we try to ask for a substring(0, 1)
from an empty string with size = 0
, so we would get a StringIndexOutOfBoundsException
. To avoid this, we can first check and confirm that the size of searchPhrase
is > 0
.
正如@ray 所说,检查异常消息表明问题发生在 searchPhrase 为空字符串时。在这种情况下,我们尝试substring(0, 1)
从空字符串中请求 a size = 0
,因此我们会得到 a StringIndexOutOfBoundsException
。为避免这种情况,我们可以先检查并确认 的大小searchPhrase
为> 0
。
Therefore, a quick fix is to replace the line if (mots.get(j).startsWith(searchPhrase.substring(0, 1))) {
to if (searchPhrase.length() > 0 && mots.get(j).startsWith(searchPhrase.substring(0, 1))) {
.
因此,快速解决方法是将行替换if (mots.get(j).startsWith(searchPhrase.substring(0, 1))) {
为if (searchPhrase.length() > 0 && mots.get(j).startsWith(searchPhrase.substring(0, 1))) {
。
Similarly, you might want to change if (count == mots.size()) {
to if (searchPhrase.length() > 0 && count == mots.size()) {
.
同样,您可能希望更改if (count == mots.size()) {
为if (searchPhrase.length() > 0 && count == mots.size()) {
.
I have modified the above two lines and can run the code without exception. The output is:
我修改了上面两行代码,可以无一例外的运行代码了。输出是:
-
-
-
-
deux
-
-
-
un
-
-
-
-
-
回答by Pim de Witte
First of all, congrats on getting started on Java. It's an amazing language and the possibilities are endless. Here is how you can go about fixing your error.
首先,恭喜您开始使用 Java。这是一种了不起的语言,它的可能性是无限的。以下是修复错误的方法。
The first step is to understand exactly what your error means, so let's get to that!
第一步是准确理解您的错误意味着什么,所以让我们开始吧!
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
The type of exception thrown is a StringIndexOutOfBoundsException. Anytime you get an IndexOutOfBoundsException (or any type thereof) it means that you are trying to access an index in an array that doesn't exist. By calling the substring
method, you are dividing the string in a character array, and selecting characters A to B (In your case 0 to 1). If character 1 doesn't exist, and yet you try to access it, it will throw this error.
抛出的异常类型是 StringIndexOutOfBoundsException。任何时候您收到 IndexOutOfBoundsException(或其任何类型)都意味着您正在尝试访问不存在的数组中的索引。通过调用该substring
方法,您将字符串划分为一个字符数组,并选择字符 A 到 B(在您的情况下为 0 到 1)。如果字符 1 不存在,但您尝试访问它,它将抛出此错误。
The reason you are getting this error is therefore that you are trying to execute a substring(0,1)
on a String with less than 1 character, aka an empty string or even a null string maybe.
因此,您收到此错误的原因是您试图对substring(0,1)
少于 1 个字符的字符串执行 a ,也可能是空字符串甚至空字符串。
Also, to give you a quick tip. Typically, a you should create a new method if your method body extends 12 lines. This will hugely increase the readability of your code. It took me a while to find the right place for your fix :-)
另外,给你一个快速提示。通常,如果您的方法主体扩展 12 行,您应该创建一个新方法。这将极大地提高代码的可读性。我花了一段时间才找到适合您修复的地方:-)
Change
改变
if (mots.get(j).startsWith(searchPhrase.substring(0, 1))) {
to
到
if (searchPhrase != null && searchPhrase.length() > 0 && mots.get(j).startsWith(searchPhrase.substring(0, 1))) {
By doing this you check if the length of your string is sufficient before executing the substring method
通过这样做,您可以在执行 substring 方法之前检查字符串的长度是否足够