java.lang.StringIndexOutOfBoundsException:String index超出范围:1 [英] java.lang.StringIndexOutOfBoundsException: String index out of range: 1

查看:1444
本文介绍了java.lang.StringIndexOutOfBoundsException:String index超出范围:1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Java,我不知道这里有什么问题。为什么会出现此错误?当我在第二个for循环之前写入count = 0行时,我看不到任何错误,实际上是有效的。



这是错误:java.lang.StringIndexOutOfBoundsException:String索引超出范围:1



这是发生错误的行:

  if(mots.get(j).startsWith(searchPhrase.substring(0,1))){

以下是整个代码:

  import java.util。*; 

public class Test {
public static void main(String [] args){
列表< String> mots = new ArrayList<();
列表< String> saya = 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 = - ; // Temque fazer cast para char depois
String frase = searchPhrase; (int i = 0; i< frase.length(); i ++){
count = 0;
(int j = 0; j< mots.size(); j ++){
if(mots.get(j).startsWith(searchPhrase.substring(0,1))){$ (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; (int g = 0; g< Saida.size(); g ++){
System.out.println(saya.get(g))
}
}
;
}
}
}


解决方案

首先,恭喜开始使用Java。这是一个惊人的语言,可能性是无止境的。以下是如何解决您的错误。



第一步是准确了解错误的含义,让我们来看看!



java.lang.StringIndexOutOfBoundsException:String索引超出范围:1



抛出的异常类型是StringIndexOutOfBoundsException。任何时候你得到一个IndexOutOfBoundsException(或任何类型的),这意味着你正在尝试访问不存在的数组中的索引。通过调用子串方法,您将字符串划分为字符数组,并选择字符A到B(在您的情况下为0到1)。如果字符1不存在,但是您尝试访问它,则会抛出此错误。



因此,您收到此错误的原因是您正在尝试在字符串上执行子字符串(0,1),字符串小于1,也可以是空字符串,甚至是空字符串。



另外,给你一个快速的提示。通常,如果方法体扩展12行,则应该创建一个新方法。这将大大增加您的代码的可读性。花了一段时间找到适合您修复的地方: - )



更改

  if(mots.get(j).startsWith(searchPhrase.substring(0,1))){

  if(searchPhrase!= null&& searchPhrase.length()> 0 && mots.get(j).startsWith(searchPhrase.substring(0,1))){

通过这样做,您可以检查字符串的长度是否足够,然后再执行子串方法


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.

That is the error: java.lang.StringIndexOutOfBoundsException: String index out of range: 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)); 
        }
    }
}

解决方案

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.

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

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.

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.

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 :-)

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

这篇关于java.lang.StringIndexOutOfBoundsException:String index超出范围:1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆