簡體   English   中英

Java索引超出范圍異常

[英]Java Index Out of Bounds Exception

我在要求我自定義功能的行上收到IndexOutOfBoundsExceptionmotCars = remplaceTirets(motAlea, motCars, lettreDon); 如果給定字母等於單詞中的字母,則此函數應該將一個或多個破折號轉換為字母),並在實際函數中將其表示為一行: tempo += tirets.charAt(j);

結果是:_ _ _ _ _ _ _(這些破折號的數量取決於程序選擇的單詞,該單詞有效,然后要求給出字母,但是當我給出字母時,我得到:

線程“ main”中的異常java.lang.String IndexOutOfBoundsException。 字符串索引超出范圍: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               

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

您正在基於tirets的長度訪問tiretsmotAlea1 我希望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; 
}

在此循環中:

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短,因此您試圖檢索超出字符范圍的字符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM