查找字符串的所有子 - 的StringIndexOutOfBoundsException [英] Find all substrings of a string - StringIndexOutOfBoundsException

查看:213
本文介绍了查找字符串的所有子 - 的StringIndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的类的话语。 Word有一个构造函数一个字符串参数和一个方法getSubstrings它返回一个包含所有子字,按长度排序的字符串。

例如,如果用户提供了输入朗姆酒,则该方法返回一个 字符串,将打印这样的:

 研究
ü
米
RU
UM
朗姆酒
 

我要连接的子字符串的字符串,它们与一个换行符(\ N)分离。然后返回字符串。

code:

 公共类字{
    串词;

    公共字(串词){
        this.word =字;
    }
    / **
     *获取这个词的所有子。
     *这个词@返回所有子分离的换行符
     * /

    公共字符串getSubstrings()
    {
        字符串str =;
        INT I,J;
        对于(i = 0; I< word.length();我++){
            为(J = 0; J< word.length(); J ++){
                海峡= word.substring(I,I + J);
                STR + =\ N的;
            }
        }
        返回海峡;
    }
 

但它抛出异常:

  java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1
    在java.lang.String.substring(String.java:1911)
 

我停留在这一点上。也许,你必须按照这个方法的签名其他建议公共字符串getSubstrings()
如何解决这个问题呢?

解决方案

异常分析:

java7目录文档

 公共类的StringIndexOutOfBoundsException扩展IndexOutOfBoundsException异常
 

  

由String方法抛出,指示索引或者为负或大于字符串的大小

Java 7的文档子

 公共字符串子(INT beginIndex,INT endIndex的)
 

  

返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的beginIndex,并扩展到endIndex字符索引 - 1。这样子串的长度为endIndex的-beginIndex。

我想这:的子串的长度为endIndex的-beginIndex 进入字符串索引超出范围:-1 。我测试的多个案例抱着我的假设正确的,但美联社preciate任何其他证据。

有关-1:糖酒会.substring(2,1); 会给你字符串索引超出范围:-1

 参数:
    beginIndex  - 该开始索引(包括)。
    endIndex的 - 结束索引,排他的。
 

原因的StringIndexOutOfBoundsException的

在给定的code段,正在试图获取字符串,它具有 endIndex的多字符串的总长度( I + J 将超过字符串的总长度):

 海峡= word.substring(I,I + J);
 

考虑这样的情况,当我= 2和j = 2字糖酒会

然后海峡= word.substring(2,4); 将是不可能的

解决方案类似于问题给出code片断:

这应该解决的问题:

 公共字符串getSubstrings()
    {
        字符串str =,SUBSTR =;
        的for(int i = 0; I< word.length();我++){
            对于(INT J = 0; I + J< = word.length(); J ++){//添加I + J和等于比较
               SUBSTR = word.substring(J,I + J); //改变word.substring(I,I + J),以word.substring(J,I + J)
               如果(等于(SUBSTR))继续; //删除空字符串
               STR + = SUBSTR; //添加串联+操作
               STR + =\ N的;
            }
        }
        返回STR +字;
    }
 

测试案例:

有关字=糖酒会,这会给输出:

 研究
ü
米
RU
UM
朗姆酒
 

I created class Word. Word has a constructor that takes a string argument and one method getSubstrings which returns a String containing all substring of word, sorted by length.

For example, if the user provides the input "rum", the method returns a string that will print like this:

r
u
m
ru
um
rum 

I want to concatenate the substrings in a String, separating them with a newline ("\n"). Then return the string.

Code:

    public class Word {
    String word;

    public Word(String word) {
        this.word = word;
    }
    /**
     * Gets all the substrings of this Word.
     * @return all substrings of this Word separated by newline
     */

    public String getSubstrings()
    {
        String str = "";
        int i, j;
        for (i = 0; i < word.length(); i++) {
            for (j = 0; j < word.length(); j++) {
                str = word.substring(i, i + j);
                str += "\n";
            }
        }
        return str;
    }

But it throws exception:

java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1911)

I stuck at this point. Maybe, you have other suggestions according this method signature public String getSubstrings().
How to solve this issue?

解决方案

Analysis of Exception:

From Java7 Docs of StringIndexOutOfBoundsException

public class StringIndexOutOfBoundsException extends IndexOutOfBoundsException

Thrown by String methods to indicate that an index is either negative or greater than the size of the string.

From Java 7 Docs of substring

public String substring(int beginIndex,int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

I guess this: length of the substring is endIndex-beginIndex comes into String index out of range: -1. I have tested with multiple cases holding my assumption true but appreciate any other proof.

For -1: "rum".substring(2,1); will give you String index out of range: -1

Parameters:
    beginIndex - the beginning index, inclusive.
    endIndex - the ending index, exclusive.

Cause of StringIndexOutOfBoundsException:

In the given code snippet, substring is trying to fetch string which has endIndex more than the total length of String (i+j will exceed the total length of string):

str = word.substring(i, i + j);

Consider the case when i=2 and j=2 for word "rum"

then str=word.substring(2, 4); would not be possible

Solution similar to code snippet given in Question:

This should solve the problem:

 public String getSubstrings()
    {
        String str="",substr = "";
        for (int i = 0; i < word.length(); i++) {
            for (int j = 0; i+j <= word.length(); j++) { //added i+j and equal to comparison
               substr = word.substring(j, i + j); //changed word.substring(i, i + j) to word.substring(j, i + j)
               if("".equals(substr))continue; //removing empty substrings
               str += substr; //added concatenation + operation
               str += "\n";
            }
        }
        return str+word;
    }

Test Case:

For word="rum", this will give output:

r
u
m
ru
um
rum

这篇关于查找字符串的所有子 - 的StringIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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