2013-04-08 80 views
1

我得到一个IndexOutOfBoundsException在要求我的自定义功能的行: motCars = remplaceTirets(motAlea, motCars, lettreDon);。 这个功能应该把破折号成信的一个或更多,如果给定的字母等于在字中的字母),并在实际功能的线,它说: tempo += tirets.charAt(j);Java索引出界异常

结果是:_ _ _ _ _ _ _(这些短线的量取决于该程序,其中工程选择的单词,然后它要求给了一封信,但是当我给了一封信,我得到:

异常在线程的主'java.lang.String IndexOutOfBoundsException。String索引超出范围:1.

因为我住在魁北克省,所以一部分是法文。但我希望这并不重要,因为法语单词只涉及字符串和单词,而不涉及java的逻辑。我是一个初学者,并且在所有关于Java的论坛上提供了所有的建议。任何具体的建议将受到欢迎。

提前感谢您花时间看看!

阿妮塔

import java.util.Scanner; 

class Tp { 

public static void main(String[] args) throws Exception { 

Scanner clavier = new Scanner(System.in); 
String motAlea = "";         
String motCars = "";     
char lettreDon = Character.UNASSIGNED;    
String tempo = "";         
String invite = ""; 
String tirets = ""; 
int l = 0;         
int m = 0;        

final String ANNONCE = "J'ai choisi un mot a deviner\n"; 
final String INSTRUCT = "Entrez une lettre a la fois. L'indice d'essais: 15.\n"; 
final String INVITE = "\tEntrez une lettre: "; 
final String BRAVO = "BRAVO ! Le mot a deviner etait: "; 
final String DESOLE = "DESOLE...Vous avez perdu..."; 

String[] vingtMots = { "WATTHEUREMETRE", "HELIOGRAPH", "GRENOUILLERE",  "CONTRAROTATIF", 
         "CUISSARDE", "BRIGANTINE", "AVITAILLEUR", "ENTREDOUBLURE", 
         "GALLETAGE", "OEUILLERE", "CREMAILLERE", "HALTEROPHILIE", 
         "MARTINGALE", "EMPENNAGE", "ENCOCHAGE", "DECLENCHEUR", 
         "BIMETALLIQUE", "PIVOTEMENT", "DECLINAISON", "CROISILLON" 
         }; // tableau string   

int indexAlea = 0; 
indexAlea = (int)(Math.random() * 20) + 1; 

motAlea = vingtMots[indexAlea]; 

for (l = 0; l < motAlea.length(); l++) { 
    tempo += "_"; 
    motCars = tempo; 
} // for 

System.out.print(ANNONCE); 
System.out.print(INSTRUCT); 
l = 0; 

do { 
    if (motCars.equals(motAlea)) { 
    System.out.print(BRAVO + motAlea + ", " + "devine en " + m + 
    " tentatives"); 
    System.exit(0); 
    } // if 

    if (l == 15) { 
     System.out.print("\n" + DESOLE + "Le mot a devine etait: " + 
     motAlea + ". " + "Au revoir... "); 
     System.exit(0); 
    } // if 

    for (int i = 0; i < motAlea.length(); i++) { 
    System.out.print(motCars.charAt(i) + " "); 
    } // for 

    m = l + 1; 
    invite = "\t" + INVITE + m + "> :"; 

    lettreDon = lecture(invite); 

    motCars = remplaceTirets(motAlea, motCars, lettreDon); 

    l++;  

} // do 
    while (l < 16); { 
    System.out.print("\n" + DESOLE + "Le mot a devine etait: " + motAlea + ". " 
    + "Au revoir... "); 
    } // while 


} //main(...) 

public static char lecture(String invite1){ 

Scanner clavier = new Scanner(System.in); 
final String ERREUR = "La valeur entree est erronnee !\nReprenez-vous..."; 
final String VIDE = " "; 
String retour = ""; 

    do { 
     try { 
     System.out.print(invite1); 
     retour = clavier.nextLine().trim(); // Mise en forme; 

      for (int k = 0; k < retour.length(); k++) { 

       if(Character.isLetter(retour.charAt(k))) { 
       return retour.toUpperCase().charAt(0); 
       } // if 
      } // for 
     } // try 
     catch (Exception e) { 
     System.out.print(ERREUR); 
     }    
    }// do 
    while (!retour.equals(VIDE)); { 
    retour = "X"; 
    return retour.charAt(0); 
    } // while    
} // lecture(...) 

public static String remplaceTirets(String motAlea1, String tirets, 
char lettre) { 
String retour; 
String tempo = ""; 

    for (int j = 0; j < motAlea1.length(); j++) { 

     String lettre1 = Character.toString(lettre); 
     if (motAlea1.charAt(j) != lettre1.charAt(0)) { 
     tempo += tirets.charAt(j); 
     } // if  
      else { 
      tempo += lettre1.charAt(0); 
      } // else 
    tirets = tempo; 
    } // for  
    return retour = tirets;   
} //remplaceTirets(...) 

}//Tp    
+2

请提供**确切的错误/例外**,全面追溯。 – 2013-04-08 03:21:28

+1

@Anita如果你减少发布的代码量,你会趋于更好(和更快)的帮助。如果你可以删除你知道不属于问题的代码,你应该,因为它会帮助别人更快地找到你的问题。 – amccormack 2013-04-08 03:27:00

+0

如果您在charAt上获取IndexOutOfBoundsException,这是因为字符索引值超出了索引字符串的范围。如果轮胎比motAlea1短,就会发生这种情况。 – 2013-04-08 03:35:41

回答

1

线

tirets = tempo; 

应该出的for循环。

更改您的代码

for (int j = 0; j < motAlea1.length(); j++) { 
    String lettre1 = Character.toString(lettre); 
    if (motAlea1.charAt(j) != lettre1.charAt(0)) { 
     tempo += tirets.charAt(j); 
    } // if  
     else { 
     tempo += lettre1.charAt(0); 
    } // else 
    //tirets = tempo; //REMOVE THIS LINE 
} // for 
tirets = tempo; //ADD THIS LINE 
+0

谢谢大家的回答... su-:那个工作..所以这个问题可以被认为是回答...当我熟练的时候,我肯定会花时间来回答这些新问题。 – Anita 2013-04-08 21:34:46

+0

没问题。祝你好运。 – 2013-04-09 02:14:31

0

您是基于motAlea1长度访问在tirets的位置。我期待motAlea1.length() > tirets.length()

for (int j = 0; j < motAlea1.length(); j++) { 

    String lettre1 = Character.toString(lettre); 

    if (motAlea1.charAt(j) != lettre1.charAt(0)) { 
     tempo += tirets.charAt(j); //THIS COULD FAIL!!! 
    }else{ 
     tempo += lettre1.charAt(0); 
    } 
    tirets = tempo; 
} 
+0

谢谢大家的回答...我会继续寻找适合速度+ = tirets.charAt(0)的线。完整的错误如下: – Anita 2013-04-08 21:18:19

0

在这个循环中:

for (int j = 0; j < motAlea1.length(); j++) { 

    String lettre1 = Character.toString(lettre); 
    if (motAlea1.charAt(j) != lettre1.charAt(0)) { 
    tempo += tirets.charAt(j); 
    } // if  
     else { 
     tempo += lettre1.charAt(0); 
     } // else 
tirets = tempo; 
} // for  

字符串tiretsmotAlea1短,所以你尝试检索字符超出其结束

+0

完整的异常:线程“main”中的异常java.lang.StringIndexOutOfBoundsException字符串超出范围:1 – Anita 2013-04-08 21:22:35