为什么在运行此程序时出现此java.lang.StringIndexOutOfBoundsException错误?

时间:2013-02-24 00:31:16

标签: java

    String fullWord;
    String firstWord;
    String secondWord;
    String thirdWord;
    String fourthWord;

    int firstPositionOfAsterisk;
    int secondPositionOfAsterisk;
    int thirdPositionOfAsterisk;
    int fullWordCharacters;
    int firstWordCharacters;
    int secondWordCharacters;
    int thirdWordCharacters;
    int fourthWordCharacters;

    char lastLetterFirstWord;


    // I will prompt the user to enter four words seperated by a *
    fullWord = JOptionPane.showInputDialog("Please enter four words: ");

    // I will use the position of the * to make things easier
    firstPositionOfAsterisk = fullWord.indexOf("*");

    firstWord = fullWord.substring(0, firstPositionOfAsterisk);

    secondPositionOfAsterisk = firstWord.indexOf("*");

    secondWord = fullWord.substring(firstPositionOfAsterisk + 1, secondPositionOfAsterisk);

    thirdPositionOfAsterisk = secondWord.indexOf("*");

    thirdWord = fullWord.substring(secondPositionOfAsterisk + 1, thirdPositionOfAsterisk);

    fourthWord = fullWord.substring(thirdPositionOfAsterisk + 1);

    firstWordCharacters = firstWord.length();
    System.out.println(firstWord +" has a length of " + firstWordCharacters + " characters" );


    secondWordCharacters = secondWord.length();
    System.out.println(secondWord +" has length of " + secondWordCharacters + " characters" );

    thirdWordCharacters = thirdWord.length();
    System.out.println(thirdWord +" has length of " + thirdWordCharacters + " characters" );

    fourthWordCharacters = fourthWord.length();
    System.out.println(fourthWord +" has length of " + fourthWordCharacters + " characters" );

    lastLetterFirstWord = firstWord.charAt(firstPositionOfAsterisk - 1);
    System.out.println("The last letter of " + firstWord + "is " + lastLetterFirstWord);

    fullWord = firstWord + secondWord + thirdWord + fourthWord;

    fullWordCharacters = fullWord.length();
    System.out.println(firstWord +", " + secondWord + ", " + thirdWord + ", " + fourthWord + "has length of" + fullWordCharacters);

我试图让用户输入由“*”分隔的4个单词,例如She *将*调用*返回,我想要像这样的输出

她的长度为3 将长度为4 通话长度为4 后面有4个长度

*符号位于:3,8和13

她的最后一个角色是s

她,将,呼唤和回来的长度是15

但是我一直得到这个java.lang.StringIndexOutOfBoundsException错误。我该如何解决这个问题?

这一行崩溃了程序

secondWord = fullWord.substring(firstPositionOfAsterisk + 1,secondPositionOfAsterisk);

2 个答案:

答案 0 :(得分:0)

简单..读这个.. String.substring(int, int)

你会看到

throws
   IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

重要的部分是 或者beginIndex大于endIndex

可能你的secondPositionOfAsterisk得到负值(-1)

答案 1 :(得分:0)

firstWord = fullWord.substring(0, firstPositionOfAsterisk);

上面将字符串的第一部分带到第一个星号,因此它不会包含星号。所以这将返回-1

secondPositionOfAsterisk = firstWord.indexOf("*");

然后这将失败:(因为-1不是有效索引)

secondWord = fullWord.substring(firstPositionOfAsterisk + 1, secondPositionOfAsterisk);

我想这个:

secondPositionOfAsterisk = firstWord.indexOf("*");

应该是

int offset = firstWord.length()+1;
secondPositionOfAsterisk = firstWord.indexOf("*", offset);

或类似的东西。

我个人认为String#split是一种更好的选择。