Téléchargement
illégal
Posez votre question Signaler

Exeption en java

master - Dernière réponse le 13 janv. 2012 à 19:33
Bonjour,
je fait ce code mais il ma affiche des errerus svp aide moi à le corriger il y a un probleme d exeption
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication2;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.lang.IndexOutOfBoundsException;

/**catch (ArrayIndexOutOfBoundsException _)
 *
 * @author marwa
 */
public class heuristique1 {
   // lire un  document
 public static ArrayList<String> lire_doc(String lex){
		ArrayList<String> wordList =new ArrayList<String>();
		try{
			InputStream ips = new FileInputStream (lex);
			InputStreamReader ipsr = new InputStreamReader (ips);
			BufferedReader br = new BufferedReader(ipsr);
			StreamTokenizer st = new StreamTokenizer(br);
			int token;
			while((token=st.nextToken())!=StreamTokenizer.TT_EOF){
				if(token==StreamTokenizer.TT_WORD){
					wordList.add(st.sval);
				}
			}
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return wordList;
	}

//lire un fichier line par line ..
    public static  void affiche(String fichier) throws IOException {
		String filePath = "C:\\"+fichier +".txt";
         ArrayList <String> m1 = new ArrayList <String>();
     ArrayList<Integer> m2= new ArrayList<Integer>();
ArrayList<Integer>indice = new ArrayList<Integer>();
try{
// Création du flux bufférisé sur un FileReader, immédiatement suivi par un
// try/finally, ce qui permet de ne fermer le flux QUE s'il le reader
// est correctement instancié (évite les NullPointerException)
BufferedReader buff = new BufferedReader(new FileReader(filePath));

try {
   // svp je veux à chaque fois qu 'il lire la ligne et le couper nombre par nombre et le met dans m1 et le deusieme dans m2
String line;
// Lecture du fichier ligne par ligne. Cette boucle se termine
// quand la méthode retourne la valeur null.
while ((line = buff.readLine()) != null) {
//System.out.println(line);win declaration mta3 m1?
    m1=lire_doc(line);
    
    // <editor-fold defaultstate="collapsed" desc="comment">
    /*indice [0]=1;
for (int i=1;i<=m1.size;i++){
int j =indice[i-1];

indice[i] =indice[i-1]+(2*m1[j])+1;

}*/
  for (int i = 1; i <= indice.size(); i++){
  m2.add(Integer.parseInt(m1.get(i)));

  }

    indice.add(1);


    for (int i = 1; i <= indice.size(); i++) {
        int j = indice.get(i - 1);
       int l = indice.get(i - 1) + (2 * m2.get(j))+ 1;// </editor-fold>

        indice.add(i, l );
//indice[i] =indice[i-1]+(2*tab[j])+1;

}
    //afficher les tableau
for (int h = 0;h<m2.size();h++){
System.out.println(m2.get(h));}
    for (int k = 0;k<indice.size();k++){
System.out.println(indice.get(k));}


    }
} finally {
// dans tous les cas, on ferme nos flux
buff.close();
}
} catch (IOException ioe) {
// erreur de fermeture des flux
System.out.println("Erreur --" + ioe.toString());
}}

    public  static  void main(String[] arg) throws IOException{
        affiche("marwa");
}}

le message de l execution est
5 1 2 6 1 3 1 1 1 2 2 2 6 4 6 3 6 5 2 6 1 1 (Le fichier spécifié est introuvable)
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at javaapplication2.heuristique1.affiche(heuristique1.java:82)
at javaapplication2.heuristique1.main(heuristique1.java:106)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
le fichier est sous c:
5 1 2 6 1 3 1 1 1 2 2 2 6 4 6 3 6 5 2 6 1 1
5 1 2 6 2 3 4 6 2 3 6 5 2 6 1 1 3 3 4 2 6 6 6 2 1 1 5 5
5 3 6 5 2 6 1 1 1 2 6 1 3 1 3 5 3 3 5 2 1 2 3 4 6 2
6 3 5 3 3 5 2 1 3 6 5 2 6 1 1 1 2 6 2 1 5 3 4 2 2 6 4 6 3 3 4 2 6 6 6
6 2 3 4 6 2 1 1 2 3 3 4 2 6 6 6 1 2 6 3 6 5 2 6 1 1 2 1 3 4 2
5 1 6 1 2 1 3 4 2 3 3 4 2 6 6 6 3 2 6 5 1 1 6 1 3 1
5 2 3 4 6 2 3 3 4 2 6 6 6 3 6 5 2 6 1 1 1 2 6 2 2 6 4 6
6 1 6 1 2 1 1 5 5 3 6 6 3 6 4 3 1 1 2 3 3 4 2 6 6 6 2 2 6 4 6
6 2 3 4 6 2 3 3 4 2 6 6 6 3 5 3 3 5 2 1 1 6 1 2 2 6 4 6 2 1 3 4 2
Lire la suite 

Exeption en java »

Suggestions
47 réponses
Réponse
+0
moins plus
Il faut que tu corriges la ligne :

for (int i = 1; i <= indice.size(); i++)

par

for (int i = 1; i <= m1.size(); i++)
master - 13 janv. 2012 à 11:07
bonjour je le corriger mais le meme probleme il m affiche à l execution
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
Ajouter un commentaire
Réponse
+0
moins plus
Bonjour,

Voici ce que tu fais:

- Tu ouvres un document marwa.txt situe dans C:/, jusqu'ici tout va bien
- Tu commences donc a le lire ligne par ligne.

Ce qui est ennuyeux, c'est ce que tu en fait de cette ligne:
m1=lire_doc(line);


En effet, si on regarde le code de lire_doc(String lex), on voit ceci:
InputStream ips = new FileInputStream (lex);


Ici ce que tu fais tu essayes d'ouvrir le fichier qui se situe a l'emplacement lex.
Ta premiere ligne de ton fichier etant "5 1 2 6 1 3 1 1 1 2 2 2 6 4 6 3 6 5 2 6 1 1 "
Ce n'est evidemment pas un chemin correct.

D'ou l'exception "(Le fichier spécifié est introuvable)"
master - 13 janv. 2012 à 11:09
bonjour
comment je peux le corriger moi je veux lire le fichier ligne par ligne et je veux lire chaque ligne nombre par nombre et met chaque ligne en un tableau
par exemple t1contient 5 1 2 6 1 3 1 1 1 2 2 2 6 4 6 3 6 5 2 6 1 1
t2 contient le deusieme ligne
et merci d avance
Ajouter un commentaire
Réponse
+0
moins plus
Voici le code

public class heuristique1 { 
     
    //lire un fichier line par line .. 
    public static  void affiche(String fichier) { 
        String filePath = "C:\\" + fichier + ".txt"; 
        ArrayList<List<String>> matrix = new ArrayList<List<String>>(); 
        try{ 
            // Création du flux bufférisé sur un FileReader, immédiatement suivi par un 
            // try/finally, ce qui permet de ne fermer le flux QUE s'il le reader 
            // est correctement instancié (évite les NullPointerException) 
            BufferedReader buff = new BufferedReader(new FileReader(filePath)); 

            try { 
                String line; 
                // Lecture du fichier ligne par ligne. Cette boucle se termine 
                // quand la méthode retourne la valeur null. 
                while ((line = buff.readLine()) != null) { 
                    String[] nbs = line.split(" "); 
                    matrix.add(Arrays.asList(nbs)); 
                } 
                 
                //afficher les tableau 
                for (List<String> tab : matrix){ 
                    System.out.println(tab); 
                } 

            } finally { 
                // dans tous les cas, on ferme nos flux 
                buff.close(); 
            } 
        } catch (IOException ioe) { 
            // erreur de fermeture des flux 
            System.out.println("Erreur --" + ioe.toString()); 
        } 
    } 

    public  static  void main(String[] arg) throws IOException{ 
        affiche("marwa"); 
    } 
     
}
master - 13 janv. 2012 à 14:55
je le corrige ce juste comme ca ??
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication2;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.String;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author marwa
 */
public class fichier {

    void decode(List<String> tab, String[][] A)
{
String[] new_tab = (String[]) tab.toArray();

int n=6;
        String m=new_tab[0];
      int f=  Integer.parseInt(m);



        int indice[]=new int[10];
indice [0]=1;
// tableau de indice 
for (int i=1;i<=f;i++){
int j =indice[i-1];
indice[i] =indice[i-1]+(2*Integer.parseInt(new_tab[j]))+1;

}
for (int j=0;j<f;j++){
System.out.println(indice[j]);

}
for(int i = 0; i < indice.length; i++){
for (int k=indice[i]+1;k<indice[i+1];k++)
{
System.out.println(new_tab[k] );

}

}
//pour le matrice qui est extraire de premier tableau 
for (int j=0;j<n;j++){
for (int g=indice[j]+1;g<indice[j+1];g=g+2){
A[g][j]=new_tab[g+1];

}

}
for (int nc =1;nc< f;nc++)
{

for (int nl=1;nl<n;nl++)
{
//System.out.println(A[nl][nc]);
}
}


    }
     
    //lire un fichier line par line .. 
    public static  void affiche(String fichier) { 
        String filePath = "C:\\" + fichier + ".txt"; 
        ArrayList<List<String>> matrix = new ArrayList<List<String>>(); 
        try{ 
            // Création du flux bufférisé sur un FileReader, immédiatement suivi par un 
            // try/finally, ce qui permet de ne fermer le flux QUE s'il le reader 
            // est correctement instancié (évite les NullPointerException) 
            BufferedReader buff = new BufferedReader(new FileReader(filePath)); 

            try { 
                String line; 
                // Lecture du fichier ligne par ligne. Cette boucle se termine 
                // quand la méthode retourne la valeur null. 
                while ((line = buff.readLine()) != null) { 
                    String[] nbs = line.split(" "); 
                    matrix.add(Arrays.asList(nbs)); 
                } 
                 
                //afficher les tableau 
                for (List<String> tab : matrix){ 
                    System.out.println(tab);
                    //appel de la methode decode 
                    decode( tab);
                } 

            } finally { 
                // dans tous les cas, on ferme nos flux 
                buff.close(); 
            } 
        } catch (IOException ioe) { 
            // erreur de fermeture des flux 
            System.out.println("Erreur --" + ioe.toString()); 
        } 
    } 
 

// A chaque fois que tu veux recuper un element de new_tab, n'oublie pas de faire ceci:
// int exemple = Integer.ParseInt(new_tab[0]);


    public  static  void main(String[] arg) throws IOException{ 
        affiche("marwa"); 
        
        
    
}
     
}

matthoffman- 13 janv. 2012 à 15:06
Oui voila, maintenant ton code est integre avec la lecture du fichier.
master - 13 janv. 2012 à 15:09
mais ce pas correcte oui ou non ?
Ajouter un commentaire
Réponse
+0
moins plus
Comment tu veux que je le saches ??? C'est ton mini projet, c'est a toi de savoir si c'est correct.

De plus, tu n'as pas besoin de mettre ta matrice en argument, tu fais comme ceci:
static void decode(List<String> tab) 
 { 
  String[] new_tab = (String[]) tab.toArray(); 

  int n=6; 
  String m=new_tab[0]; 
  int f=  Integer.parseInt(m); 
  int[][] A = new int[n][Integer.parseInt(m)]; 

.... 
}


Comme je te l'ai dit (mais tu as juste recopie mon commentaire sans apporter les modifications ...), si tu veux utiliser les chiffres dans le tableau, tu dois utiliser "Integer.parseInt", parce que sinon c'est des String que tu manipules ...
master - 13 janv. 2012 à 19:18
mais moi je veux prendre les valeurs de tab qui sont entre le les deux élément successive de tableau indice
par exemple indice contient 1479
moi j afficher le element de tableau tab qui sont tab[2]... tab[4]
master - 13 janv. 2012 à 19:32
je vous propose une chose en peux essai à corriger ce code est je suis sur que il va marche car le proleme est dans le boucle
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author marwa
 */
package javaapplication2;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class heuristique {
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/





/**
*
* @author marwa
*/




/**
* @param args the command line arguments
*/
public static void main(String[] args)throws IOException {



int tab[]={ 5 , 1 ,2 ,6 ,1, 3, 1 ,1, 1 ,2, 2, 2, 6 ,4, 6 ,3 ,6 ,5, 2, 6, 1, 1 };
int n=6;
int m=tab[0];
int nbc=tab[0];
int nbl=6;
int[][] A = new int[n][m];
int indice[]=new int[10];
indice [0]=1;
for (int i=1;i<=nbc;i++){
int j =indice[i-1];

indice[i] =indice[i-1]+(2*tab[j])+1;

}
for (int j=0;j<nbc;j++){
System.out.println(indice[j]);

}
for(int i = 0; i < indice.length; i++){
for (int k=indice[i]+1;k<indice[i+1];k++)
{
System.out.println(tab[k] );

}

}
for (int j=0;j<n;j++){
    for (int l=0;l<5;l++){
    A[j][l]=0;
    }
}

for (int j=0;j<n;j++){
for (int g=indice[j]+1;g<indice[j+1];g=g+2){
A[g][j]=tab[g+1];


}

}
for (int nc =1;nc< nbc;nc++)
{

for (int nl=1;nl<nbl;nl++)
{
System.out.println(A[nl][nc]);
System.out.println();

}
}
}
}

master - 13 janv. 2012 à 19:33
le avant dernier for ne marche pas je car lorsque je le tester avec system.out .println elle n affiche rien
Ajouter un commentaire
Ce document intitulé « exeption en java » issu de CommentCaMarche (www.commentcamarche.net) est mis à disposition sous les termes de la licence Creative Commons. Vous pouvez copier, modifier des copies de cette page, dans les conditions fixées par la licence, tant que cette note apparaît clairement.
Dossier à la une
Passage au tout numérique : quel coût pour les particuliers ?