Recherche dans un fichier puis transfert dans un tableau

fallpoint - 11 janv. 2013 à 16:32
 fallpoint - 18 janv. 2013 à 18:01
Bonjour je veut un programme qui doit faire ceci:
l'utilisateur saisi des mots séparés par des espaces dans un textfield
le programme vérifie dans un fichier.txt si chaque mots saisies existe dans le fichier si oui il le met dans un tableau.pour vérifier si sa marche il m'affiche les mots.
voici le programme que j'ai écris mais sa ne marche pas.Si quelqu'un peu m'aider svp

public String[] tadjectif = new String[Maxnbmot];
 String adjectifs = "G:\\Users\\mohamet fall\\Documents\\NetBeansProjects\\Phrase\\adjectifs.txt";
              try{
                  
InputStream ips=new FileInputStream(adjectifs); 
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String ligne;
                        String[] result = motstfd.getText().split("\\s");
while ((ligne=br.readLine())!=null){
                           StringTokenizer st=new StringTokenizer(ligne);
                               while(st.hasMoreTokens()){
                 
                                   for ( int x=0; x<result.length; x++){
                                   if(st.nextToken().equals(result[x])){         
                                       
                                       tadjectif[x] = result[x];
                                       
                                       
                                       }
                                   }
                                   System.out.println(tadjectif[x]);
                                  
                               }                
chaine+=ligne+"\n";
}
br.close(); 
}		 

catch (Exception e){
        	System.out.println(e.toString());
} 

5 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
14 janv. 2013 à 08:58
Salut,

Peux tu plutôt afficher la trace de ton erreur et nous la recopier stp ?

catch (Exception e){
        	e.printStackTrace()
} 
0
bonjour julien merci pour l'intervention. Mais entre temps j'ai eu une solution qui marche pour moi, comme mon fichier.txt est un dictionnaire et les lignes ne contiennent qu'un seul champ j'ai pas utiliser StirngTokeneizer
voici le code.
public String[] tadjectif = new String[Maxnbmot];
 String adjectifs = "G:\\Users\\mohamet fall\\Documents\\NetBeansProjects\\Phrase\\adjectifs.txt";
String chaine="";
String[] result = motstfd.getText().split("\\s");
try{
InputStream ips=new FileInputStream(tadjectif); 
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String ligne;
while ((ligne=br.readLine())!=null){

                            for (int x=0; x<result.length; x++){
                                   if(ligne.intern().equals(result[x])){
                                    tadjectif[x]= result[x];
                                    
                                    }
                               }                
chaine+=ligneadv+"\n";
}
br.close(); 
                                        
                         System.out.println("liste des adjectives");
                                     for (int x=0; x<result.length; x++){ 
                                        System.out.println(tadjectif[x]);
                                       }    
}	
                     			
catch (Exception e){
        	System.out.println(e.toString());
} 
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
15 janv. 2013 à 08:19
Tu devrais tout de même enlever le System.out.println(e.toString()); et le remplacer par e.printStackTrace();
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
15 janv. 2013 à 08:21
Et en général, on essaye d'éviter les catch (Exception e){

Tout ce que tu as a attraper c'est un IOException
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
merci tu as raison
public String[] tadjectif = new String[Maxnbmot];
 String adjectifs = "G:\\Users\\mohamet fall\\Documents\\NetBeansProjects\\Phrase\\adjectifs.txt";
String chaine="";
String[] result = motstfd.getText().split("\\s");
try{
InputStream ips=new FileInputStream(tadjectif); 
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String ligne;
while ((ligne=br.readLine())!=null){

                            for (int x=0; x<result.length; x++){
                                   if(ligne.intern().equals(result[x])){
                                    tadjectif[x]= result[x];
                                    
                                    }
                               }                
chaine+=ligneadv+"\n";
}
br.close(); 
                                        
                         System.out.println("liste des adjectives");
                                     for (int x=0; x<result.length; x++){ 
                                        System.out.println(tadjectif[x]);
                                       }    
}	
                     			
catch (Exception ex){
        	ex.PrintStackeTrace();
} 
0
Rejoignez-nous