2013-10-29 41 views
0

我正在为我的计算机科学课做一个hang子手游戏,似乎无法弄清楚如何解决我的问题与字符串。我遇到了一个越​​界索引错误。它说为什么我的字符串索引超出范围?

java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:7

在这里发生:

if(theGuess.equals(wordToGuess.substring(i,i+1))) 

下面是程序代码,如果它是任何帮助。

import javax.swing.JOptionPane; 
public class Hangman extends BasicGame 
{ 
    private final String WORDCHOICES= "apple"+"great"+"zebra"+"mouse"+"chick"+"class"+"abhor"+"abide" 
     +"fuzzy"+"brute"+"blunt"+"comic"+"cater"+"stone"+"chaos"+"dufus"+"earth"+"decal"+"happy"+"heist" 
     +"idler"+"lions"+"hates"+"idols"+"lasso"+"lives"+"lisps"+"major"+"mound"+"mango"+"meter"+"mercy" 
     +"marry"+"pilot"+"plots"+"pants"+"overt"+"quack"+"paver"+"polls"+"scorn"+"sapid"+"sails"+"rowdy" 
     +"seeks"+"leech"+"seats"+"spade"+"shoes"+"slurp"; 
    private String wordToGuess; 
    private java.util.Random randy; 

    private int wordNum; 
    private int numCorrect=0; 
    private String[] correctLetters= new String[]{"","","","",""}; 
    HangDraw artist= new HangDraw(); 
    public Hangman() 
    { 
     super(); 
     randy= new java.util.Random(); 
     for(int i = 0; i<5;i++) 
      correctLetters[i]=null; 
     wordNum=0; 
     numCorrect=0; 
     artist.setUp(); 
    } 
    public void guess() 
    { 
     wordNum= 5*randy.nextInt(50); 
     numCorrect=0; 
     int wrong=0; 
     String userGuess=""; 
     int partsDrawn=0; 
     wordToGuess=WORDCHOICES.substring(wordNum,wordNum+5)+" "; 
     while(numCorrect<5&& partsDrawn<5) 
     { 
      userGuess= JOptionPane.showInputDialog("Guess a letter, so far you have: "+ correctLetters[0]+ 
         correctLetters[1]+correctLetters[2]+correctLetters[3]+correctLetters[4]); 

      if(checkLetter(userGuess)) 
      { 
       JOptionPane.showMessageDialog(null, "Correct Guess"); 
       //print the letter 
      } 
      else 
      { 
       //draw the part of the body 
       JOptionPane.showMessageDialog(null,"incorrect"); 
       partsDrawn++; 
       artist.drawParts(partsDrawn); 
      } 
     } 
     if(partsDrawn==5) 
     { 
      JOptionPane.showMessageDialog(null, "failed to guess, the word is: "+wordToGuess); 
     } 
     else 
     { 
      JOptionPane.showMessageDialog(null, "correct, the word was: "+ wordToGuess); 
     } 
    } 

    private boolean checkLetter(String theGuess) 
    { 
     boolean matches=false; 
     for(int i=0;i<wordToGuess.length();i++) 
     { 
      if(theGuess.equals(wordToGuess.substring(i,i+1))) 
      { 
       correctLetters[i]=theGuess; 
       matches=true; 
       numCorrect++; 
      } 
     } 
     return matches; 
    } 

} 

谢谢您提供

+4

您是否知道'WORDCHOICES'是以下字符串:'applegreatzebramousechickclassabhorabidefuzzybrutebluntcomiccaterstonechaosdufusearthdecalhappyheistidlerlionshatesidolslassoliveslispsmajormoundmangometermercymarrypilotplotspantsovertquackpaverpollsscornsapidsailsrowdyseeksleechseatsspadeshoesslurp'?为什么不使用数组?那么你的话不会被限制在5个字符。 – Cruncher

+0

我不应该使用数组,我的老师想要250个字符的字符串中有50个5个字母的单词,谢谢你的建议,尽管 – abysmaldan

+2

似乎每天都有人在这里有一个由老师组成的不同的疯狂要求。如果你想教字符串操作,那么不要使用一个具有更好的数组/集合解决方案的例子。显然这不是你的错,有时候我真的不明白。 – Cruncher

回答

0

因为你明显超出范围。你不能从字符串的末尾开始子串。

for(int i=0;i<wordToGuess.length();i++) 
{ 
    if(theGuess.equals(wordToGuess.substring(i,i+1))) 
    { 
     correctLetters[i]=theGuess; 
     matches=true; 
     numCorrect++; 
    } 
} 
找出

最快的方法是调试应用程序

+1

substring的endIndex是唯一的,因此它可以等于wordToGuess.length(),您的解决方案将跳过最后一个字符。 – Daniel

+0

好的,谢谢,我仍然需要检查最后一个字符,所以我在之前的章节中将字符串连接到“wordToGuess”的末尾并修复它 – abysmaldan

+2

@abysmaldan当你所需要做的是连接空间是一个可怕的解决方案修复你的循环参数。 – jlars62

0

任何帮助的问题是,你是出界外的前面for循环:for(int i=0;i<=wordToGuess.length();i++)。这将循环超过字符串中可用字符的末尾。注意到您使用的是使用端点标记的子字符串函数,后一个端点必须位于字符串的范围内,因此应将此循环指定为for(int i=0;i<wordToGuess.length();i++)。这将确保最后一次迭代不会调用IndexOutOfBoundsError

+0

您的代码将跳过最后一个字符。 /最后编辑修正了它 – Daniel

+0

更正了,谢谢,错过了端点上Java文档中的'独占'标记。 – abiessu

3

根据Java docs,字符串#子引发IndexOutOfBoundsException

如果将beginIndex为负,或endIndex大于此String对象的长度大,或beginIndex大于endIndex。

在你的循环的最后一次迭代,i将等于字符串的length,并i+1比字符串的长度,因此异常大。

所以,你需要改变:

for(int i=0;i<=wordToGuess.length();i++) 

for(int i=0;i<wordToGuess.length();i++) 
      ^^^ 
0

i<=wordToGuess.length()for循环改变你的状况,i<wordToGuess.length(),因为现在你的循环来的那一刻,当i变最后一个元素,所以i+1自然指向“超出界限”。

0

补充以下变化

for(int i=0;i<wordToGuess.length();i++) 
+0

这为什么解决这个问题,OP的错误是什么? – Mark

1

你的循环是for(int i=0;i<=wordToGuess.length();i++)

让我们假设一个简单的例子,wordToGuess="ABC"

循环生成i = 0到i的值= 3。

对于i = 0,选择第一个字符,对于i = 1第二个,对于i = 2第三个,i = 3没有意义。

因此使用for(int i=0;i<wordToGuess.length();i++)

0

核心Java是在告诉你什么是错的真棒。你说if(theGuess.equals(wordToGuess.substring(i,i+1)))是抛出IndexOutOfBoundsException?这意味着wordToGuess长度小于一个字符(即空字符串)。基本上你试图获得长度为1 [i,i + 1)的子字符串。

因为你是学生,我认为自己完成这些工作很重要,我只是想给你一个提示:看看for循环的条件。另外,用调试器遍历代码,或者至少放入一些System.out.println(wordToGuess)语句来查看这些值是什么。