2013-02-24 215 views
-1
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个字分隔由“*”,例如她会* * *电话回来,我想输出喜欢这样为什么在运行这个程序时会出现这个java.lang.StringIndexOutOfBoundsException错误?

她有长3 会具有长度为4 呼叫具有长度4 背面具有长度4

中的*符号的位置处发现的:3,图8和13

的最后一个字符的她为s

她,将,呼叫,回来的长度是15

但我不断收到此java.lang.StringIndexOutOfBoundsException错误。我该如何解决?

这条线崩溃程序

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

回答

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

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是一种更好的选择。

相关问题