Probleme d'exception ArrayIndexOutOfBoundsException
Bonjour,
comme vous pouvez le constatez il est trés tard et ça fait plus de 4 heures que je me casse la tête avec mon @# de programme en java.
Le probléme que j'ai c'est surtout avec les bloc try et catch,je ne sais pas ou les mettre et je crois que c est l origine de mon probleme,le deuxieme probleme c'est que j ai codé au kilometre et donc ce n'est pas tres clair.
Par contre lorsque j'excecute la deuxieme partie de mon programme ca fonctionne tres bien sans aucun message d'erreur.
Je vous explique vaguement ce que fait ce programme.
Il lit un premier fichier (X),qu'il transforme en X2 puis il lit un deuxiemen fichier X3,à partir de X2(le fichier transformé ) et X3 il crée un nouveau fichier!!
En fait,le programme marche parfaitement quand je saute l'etape 1 c est a dire quand j utilise un fichier deja present X2,donc je suis sure que ca bloque au début.
Voila mon code(désolé je sais que c est 300 lignes,mais j ai mis le max de commentaire)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
| import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
class GestionCommence implements ActionListener {
public void actionPerformed(ActionEvent contexte) {
String caractere=" CIE AV MV JV NUVOL DEP ARR NBHH FDPNT FDPNC";
String tab_origin;
String tab_1;
String nom_fichier=null; //nom du fichier à ouvrir
String nom_fichier2=null; //nom du nouveau fichier qui sera créer
String nom_fichier3=null;
String nom="_traité.txt"; //nouvelle extension du fichier créer
boolean premierefois=true; //servira pour savoir si c'est la premiere fois que l'on écrit dans le nouveau fichier afin de coller avant le traitement les caractères CIE AV ...
boolean trouve=false;
//traitement pour offrir à l'utilisateur la possibilité de parcourir des dossiers afin de trouver le fichier à convertir
try{
String wd = System.getProperty("user.dir");
JFileChooser fc = new JFileChooser(wd);
int rc = fc.showDialog(null, "Choisir le fichier");
if (rc == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
nom_fichier = file.getAbsolutePath();
}
String wd1 = System.getProperty("user.dir");
JFileChooser fc1 = new JFileChooser(wd1);
int rc1 = fc1.showDialog(null, "Choisir le fichier");
if (rc1 == JFileChooser.APPROVE_OPTION)
{
File file = fc1.getSelectedFile();
nom_fichier3 = file.getAbsolutePath();
}
//manipulation pour créer le nom du nouveau fichier
nom_fichier2=nom_fichier.substring(0,nom_fichier.length()-4);
nom_fichier2=nom_fichier2.concat(nom);
//BufferedReader reader = new BufferedReader(new InputStreamReader( nom_fichier );
//String nomFichier = reader.readLine();
FileReader fr = new FileReader( nom_fichier );
BufferedReader br = new BufferedReader( fr );
String ligne = null;
//création du nouveau fichier
File fichier=new File(nom_fichier2);
fichier.createNewFile();
while( ( ligne = br.readLine() ) != null )
{
tab_origin=ligne;
//affectation de chaque ligne du fichier dans un tableau char pour pouvoir traiter l'information
char tab[]=new char[tab_origin.length()];
for (int i=0; i<tab_origin.length();i++)
{
tab[i]=tab_origin.charAt(i);
}
char tab_final[]=new char[33];//création d'un tableau qui contiendra la ligne traité,tableau que l'on ecrira directement dans le nouveau fichier
tab_final[0]=' ';
tab_final[1]=tab[22];
tab_final[2]=tab[23];
tab_final[3]=' ';
tab_final[4]=' ';
tab_final[5]=' ';
tab_final[6]=' ';
tab_final[7]=tab[25];
tab_final[8]=tab[26];
tab_final[9]=' ';
tab_final[10]=tab[27];
tab_final[11]=tab[28];
tab_final[12]=' ';
tab_final[13]=tab[29];
tab_final[14]=tab[30];
tab_final[15]=' ';
tab_final[16]=' ';
tab_final[17]=' ';
tab_final[18]=tab[32];
tab_final[19]=tab[33];
tab_final[20]=tab[34];
tab_final[21]=tab[35];
tab_final[22]=' ';
tab_final[23]=' ';
tab_final[24]=' ';
tab_final[25]=' ';
tab_final[26]=tab[36];
tab_final[27]=tab[37];
tab_final[28]=tab[38];
tab_final[29]=' ';
tab_final[30]=' ';
tab_final[31]=tab[39];
tab_final[32]=tab[40];
tab_final[33]=tab[41];
//préparation pour ecrire le tab_final dans le fichier traité
PrintWriter ecrivain;
//if(fichier.exists()==false)
// {
ecrivain=new PrintWriter(new BufferedWriter (new FileWriter(nom_fichier2,true)));
ecrivain.println(tab_final); //on écrit le tableau qui contient les données traitées dans chaque ligne du fichier
ecrivain.close();
}//fin de la boucle while qui fait le traitement ligne par ligne du fichier original
br.close();
}catch( IOException e )
{
JOptionPane.showMessageDialog(null,
"Problème inconnue lors de la lecture du fichier", "convertion",
JOptionPane.ERROR_MESSAGE);
e.getMessage();
}
try{
//on va ouvrir les deux fichiers et on va en creer un nouveau
//ouverture du premier fichier
FileReader fr1 = new FileReader( nom_fichier2 );
BufferedReader br1 = new BufferedReader( fr1 );
String ligne1 = null;
//ouverture du deuxieme fichier
//je cree le dernier fichier qui contiendra tout
File fichier_final=new File("/home/mehdi/Bureau/psss.txt");
if(!fichier_final.createNewFile())
System.out.println("okkkkkkkkkkkkkkkkkkkkkk");
PrintWriter ecrivain;
while( ( ligne1 = br1.readLine() ) != null )
{
tab_1=ligne1;
//affectation de chaque ligne du fichier dans un tableau char pour pouvoir traiter l'information
char tab1[]=new char[tab_1.length()];
for (int i=0; i<tab_1.length();i++)
{
tab1[i]=tab_1.charAt(i);
}
//je stocke ici le numéro de vol
char num_vol[]=new char[3];
num_vol[0]=tab1[18];
num_vol[1]=tab1[19];
num_vol[2]=tab1[20];
// System.out.println(num_vol);
String vol=new String(num_vol);
FileReader fr2 = new FileReader( nom_fichier3 );
BufferedReader br2 = new BufferedReader( fr2 );
String ligne2 = null;
while( ( ligne2 = br2.readLine() ) != null && trouve==false)
{
if(ligne2.startsWith(vol))
{
trouve=true;
ligne2=ligne2.substring(3,ligne2.length());
ligne2=ligne2.trim();//j'elimine les espaces
ligne1=ligne1+" "+ligne2; //7 espaces pr le nombre dheures
//je dois écrire cette ligne dans le dernier fichier
char ligne11[]=new char[ligne1.length()];
for (int i=0; i<ligne1.length();i++)
{
ligne11[i]=ligne1.charAt(i);
}
if(premierefois==true)//si c est la premiere fois que l'on écrit il faudra mettre au début du fichier CIE AV MV...
{
premierefois=false;
//traitement pour pouvoir écrire directement CIE MV... dans le fichier
char caract[]=new char[caractere.length()];
for (int i=0; i<caractere.length();i++)
{
caract[i]=caractere.charAt(i);
}
ecrivain=new PrintWriter(new BufferedWriter (new FileWriter("/home/mehdi/Bureau/psss.txt",true)));
ecrivain.println(caract);
//ecrivain.println(tab_final);
ecrivain.println(ligne11);
ecrivain.close();
JOptionPane.showMessageDialog(null,
"Convertion et création du fichier avec succès(NOM_traité.txt) !", "convertion",
JOptionPane.INFORMATION_MESSAGE);
}
else
{
ecrivain=new PrintWriter(new BufferedWriter (new FileWriter("/home/mehdi/Bureau/psss.txt",true)));
ecrivain.println(ligne11); //on écrit le tableau qui contient les données traitées dans chaque ligne du fichier
ecrivain.close();
}
}
}//fin du while qui parcout le deuxieme fichier
//je remet le booleen a false
trouve=false;
br2.close();
}//fin du while qui parcourt le premier fichier
br1.close();
}catch( IOException e )
{
JOptionPane.showMessageDialog(null,
"Problème inconnue lors de la lecture du fichier", "convertion",
JOptionPane.ERROR_MESSAGE);
e.getMessage();
}
}
} |
Et voila ce que j'obtiens comme erreurs quand j'excecute le programme:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 33
at GestionCommence.actionPerformed(GestionCommence.java:146)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2013)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2336)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:260)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:254)
at java.awt.Component.processMouseEvent(Component.java:6100)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3288)
at java.awt.Component.processEvent(Component.java:5865)
at java.awt.Container.processEvent(Container.java:2110)
at java.awt.Component.dispatchEventImpl(Component.java:4461)
at java.awt.Container.dispatchEventImpl(Container.java:2168)
at java.awt.Component.dispatchEvent(Component.java:4287)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4466)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4130)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4060)
at java.awt.Container.dispatchEventImpl(Container.java:2154)
at java.awt.Window.dispatchEventImpl(Window.java:2555)
at java.awt.Component.dispatchEvent(Component.java:4287)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:605)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:276)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:191)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:186)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:178)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:139) |
J'ai des autres classes pour le graphique qu genre fenetre et gestion des événements!!
S'il vous plait aidez moi:(:(:(:(:(:(:(:(:(