IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Entrée/Sortie Java Discussion :

Trouver un mot dans un fichier texte [Débutant(e)]


Sujet :

Entrée/Sortie Java

  1. #1
    Membre à l'essai
    Inscrit en
    Avril 2005
    Messages
    21
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 21
    Points : 17
    Points
    17
    Par défaut Trouver un mot dans un fichier texte
    Bonjour,
    Je desire trouver un mot dans un fichier texte et ma fonction ne marche pas,voici mon code...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
     
    public void trouvermot(String fich,String mot)
     
     {   String cf ="c:\\"+fich+".txt"; 
          try { 
      FileReader rd = new FileReader(cf); 
      StreamTokenizer st = new StreamTokenizer(rd); 
     
      int token = st.nextToken(); 
     
      while (token != StreamTokenizer.TT_EOF) { 
                token = st.nextToken(); 
                String word = st.sval; 
          if(word.startsWith(mot))
                    System.out.print("mot touvé"); 
     } 
        rd.close(); 
        } catch (IOException e) { 
        }
    }
    Merci pour votre aide bonne journée

  2. #2
    Membre régulier
    Profil pro
    Inscrit en
    Juin 2004
    Messages
    135
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2004
    Messages : 135
    Points : 110
    Points
    110
    Par défaut
    Va voir un petit peu plus bas dans les post!

    http://www.developpez.net/forums/viewtopic.php?t=468507

    Ben

  3. #3
    Expert éminent sénior
    Avatar de Baptiste Wicht
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2005
    Messages
    7 431
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2005
    Messages : 7 431
    Points : 21 324
    Points
    21 324
    Par défaut
    Qu'est ce qui ne marche pas, il te fait une erreur, il te donne une fausse réponse ?

  4. #4
    Membre à l'essai
    Inscrit en
    Avril 2005
    Messages
    21
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 21
    Points : 17
    Points
    17
    Par défaut
    il me dit rien cet animal

  5. #5
    Membre chevronné
    Avatar de Deadpool
    Homme Profil pro
    Inscrit en
    Novembre 2005
    Messages
    1 312
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations forums :
    Inscription : Novembre 2005
    Messages : 1 312
    Points : 2 011
    Points
    2 011
    Par défaut
    Normal qu'il ne te dise rien :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    catch (IOException e) { 
        }
    Tu ne fait rien de la catch, forcément, si y'a une exception il va pas te le dire.

    Commence déjà par ajouter ça :


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    catch (IOException e) { 
             e.printStackStrace();
        }
    Là, il sera un peu plus parlant.

    Ne jamais museler les exceptions.

  6. #6
    Membre à l'essai
    Inscrit en
    Avril 2005
    Messages
    21
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 21
    Points : 17
    Points
    17
    Par défaut
    en effet,



    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    java.lang.NullPointerException
     
            at javaapplication3.fichier.trouvermot(fichier.java:135)
     
            at javaapplication3.simulation.actionPerformed(simulation.java:61)
     
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
     
            at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)
     
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
     
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
     
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
     
            at java.awt.Component.processMouseEvent(Component.java:5100)
     
            at java.awt.Component.processEvent(Component.java:4897)
     
            at java.awt.Container.processEvent(Container.java:1569)
     
            at java.awt.Component.dispatchEventImpl(Component.java:3615)
     
            at java.awt.Container.dispatchEventImpl(Container.java:1627)
     
            at java.awt.Component.dispatchEvent(Component.java:3477)
     
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
     
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
     
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
     
            at java.awt.Container.dispatchEventImpl(Container.java:1613)
     
            at java.awt.Window.dispatchEventImpl(Window.java:1606)
     
            at java.awt.Component.dispatchEvent(Component.java:3477)
     
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:480)
     
            at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
     
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
     
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
     
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
     
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
    ??? euhhhhh!!!!!

  7. #7
    Expert éminent sénior
    Avatar de Baptiste Wicht
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2005
    Messages
    7 431
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2005
    Messages : 7 431
    Points : 21 324
    Points
    21 324
    Par défaut
    java.lang.NullPointerException ca veut dire que tu essaie de faire quelque chose sur un objet qui est null

    at javaapplication3.fichier.trouvermot(fichier.java:135) ca te dit que l'erreur est à la ligne 135. C'est quoi la ligne 135 ?

    D'apres le fichier sur lequel tu essaie de travailler n'existe pas

  8. #8
    Membre à l'essai
    Inscrit en
    Avril 2005
    Messages
    21
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 21
    Points : 17
    Points
    17
    Par défaut
    ok ca veut dire,grand merci!
    à la ligne 135
    //la premiere instruction quand on rentre dans la boucle
    token = st.nextToken();

  9. #9
    Expert éminent sénior
    Avatar de Baptiste Wicht
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2005
    Messages
    7 431
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Suisse

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2005
    Messages : 7 431
    Points : 21 324
    Points
    21 324
    Par défaut
    Ca veut donc dire que st est null, donc je pense que ton fichier est vide ou quelque chose comme ca

  10. #10
    Membre à l'essai
    Inscrit en
    Avril 2005
    Messages
    21
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 21
    Points : 17
    Points
    17
    Par défaut
    j'ai gagné aprés plusieurs heures de lutte

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     public void trouvermot(String fich,String mot)
     
    { 
    try {  
            boolean trv=false;
            String adressedufichier ="c:\\"+fich+".txt";
            FileReader rd = new FileReader(adressedufichier); 
            StreamTokenizer st = new StreamTokenizer(rd); 
     
            int token= st.nextToken(); 
            while (token != StreamTokenizer.TT_EOF) { 
                token = st.nextToken(); 
                String word = st.sval; 
     
           if((st.ttype)==StreamTokenizer.TT_WORD)
                    if(word.equals(mot))
    	        trv=true;
                                                     }
            if(trv)
                System.out.println("trouvé :\t"+mot);
            else  System.out.println("Non trouvé :\t"+mot);
            rd.close(); 
        } catch (IOException e) { 
           e.printStackTrace(); 
        }
     
     
     }
    Si ca peut servir à quelqu'un
    Merci pour votre aide en tout cas

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Trouver un mot dans un fichier texte
    Par spl0tch dans le forum Langage
    Réponses: 12
    Dernier message: 16/04/2007, 21h14
  2. trouver un mot dans un fichier
    Par samaa dans le forum Langage
    Réponses: 4
    Dernier message: 04/03/2007, 16h21
  3. Ecrire un mot dans un fichier texte existant
    Par Mickey.jet dans le forum Delphi
    Réponses: 10
    Dernier message: 26/02/2007, 23h23
  4. Réponses: 10
    Dernier message: 29/04/2006, 10h40
  5. Réponses: 4
    Dernier message: 16/12/2005, 17h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo