word is not present

word is not present - Java - Programmation

Marsh Posté le 18-05-2012 à 21:22:57    

bonjour,

Code :
  1. import java.io.*;
  2. public class Cnt1
  3. {
  4. public static void main(String[]args)throws IOException
  5. {
  6.  int times=0,count=0,x=0,no=0;
  7.  InputStreamReader ir=new InputStreamReader(System.in);
  8.  BufferedReader br=new BufferedReader(ir);
  9.  String s,w;
  10.  System.out.println("Enter the sentence:" );
  11.  s=br.readLine();
  12.  System.out.println("Enter the word:" );
  13.  w=br.readLine();
  14.  try
  15.  {
  16.   for(int i=0;i<s.length();i++)
  17.   {
  18.    if(w.charAt(0)==s.charAt(i))
  19.    {
  20.     for(int j=0;j<w.length();j++)
  21.        {
  22.      if(s.charAt(i)==w.charAt(j))
  23.     {
  24.      count=count+1;
  25.     }
  26.     if(count==w.length())
  27.     {
  28.      no=no+1;count=0;
  29.     }
  30.     else
  31.     {
  32.      System.out.println("word is present"+no+"times" );
  33.     }
  34.    }}}}
  35.    catch(Exception e){}
  36.    if(no==0)
  37.    {}
  38.    {
  39.     System.out.println("word is not present" );
  40.    }
  41.   }
  42.  }


le résultat est la suivante:
 
Enter the sentence:
je vais à paris
Enter the word:
paris
word is not present
 
aidez moi svp

Reply

Marsh Posté le 18-05-2012 à 21:22:57   

Reply

Marsh Posté le 19-05-2012 à 00:45:57    

A la base, l'erreur était ici:
if (s.charAt(i) == w.charAt(j))
il fallait faire
if (s.charAt(i+j) == w.charAt(j))
sinon on ne comparait le mot qu'avec un unique caractère de la phrase.
 
En reformatant (car ton code initial est illisible) et remettant les choses à leur place, ça donne:
 

Code :
  1. import java.io.*;
  2.  
  3. public class Cnt1 {
  4.  
  5.    public static void main(String[] args) throws IOException {
  6.        int times=0, count=0, x=0, no=0;
  7.        InputStreamReader ir = new InputStreamReader(System.in);
  8.        BufferedReader    br = new BufferedReader(ir);
  9.        String s, w;
  10.  
  11.        System.out.println("Enter the sentence:" );
  12.        s = br.readLine();
  13.        System.out.println("Enter the word:" );
  14.        w = br.readLine();
  15.  
  16.        try {
  17.            for (int i = 0; i < s.length(); i++) {
  18.                if (w.charAt(0) == s.charAt(i)) {
  19.                    for (int j = 0; j < w.length() && i+j < s.length(); j++) {
  20.                        if (s.charAt(i+j) == w.charAt(j)) {
  21.                            count++;
  22.                        }
  23.                    }
  24.                    if (count == w.length()) {
  25.                        no++;
  26.                        count = 0;
  27.                    }
  28.                }
  29.            }
  30.            if (no == 0) {
  31.                System.out.println("word is not present" );
  32.            }
  33.            else {
  34.                System.out.println("word is present "+no+" times" );
  35.            }
  36.        }
  37.        catch(Exception e) {
  38.        }
  39.    }
  40.  
  41. }


 

C:\Java>java Cnt1
Enter the sentence:
je vais a paris et paris
Enter the word:
paris
word is present 2 times


 
A+,


Message édité par gilou le 19-05-2012 à 00:56:45

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 19-05-2012 à 14:03:31    


merci beaucoup
A++


Message édité par gilou le 19-05-2012 à 16:07:49
Reply

Marsh Posté le 20-05-2012 à 15:40:25    

Bon, et maintenant, la version telle que je l'aurais écrite:

Code :
  1. import java.lang.String;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. public class ScanWord {
  7.  
  8.    public static void main(String[] args) {
  9.        System.out.print("Enter the sentence: " );
  10.        Scanner inputScanner = new Scanner(System.in);
  11.        String input = inputScanner.nextLine().trim();
  12.        if  (input.length() == 0) {
  13.            inputScanner.close();
  14.            System.out.println("Sentence entered is empty!" );
  15.        }
  16.        else {
  17.            System.out.print("Enter the word: " );
  18.            // Note: On pourrait déclencher une erreur s'il y a plus d'un mot
  19.            String word = inputScanner.nextLine().trim().split("\\s+" )[0];
  20.            inputScanner.close();
  21.            if  (word.length() == 0) {
  22.                System.out.println("Word entered is empty!" );
  23.            }
  24.            else {
  25.                List<String> wordList = Arrays.asList(input.trim().split("\\s+" ));
  26.                int occurences = 0;
  27.                for (String w : wordList) {
  28.                    // Note: ou equalsIgnoreCase selon ce qu'on veut
  29.                    if (w.equals(word)) {
  30.                        ++occurences;
  31.                    }
  32.                }
  33.                switch (occurences) {
  34.                case 0: System.out.println("no \"" + word + "\" in the sentence." );
  35.                    break;
  36.                case 1: System.out.println("\"" + word + "\" occurs once in the sentence." );
  37.                    break;
  38.                default: System.out.println("\"" + word + "\" occurs " + occurences + " times in the sentence." );
  39.                    break;
  40.  
  41.                }
  42.            }
  43.        }
  44.    }
  45.  
  46. }


 
et la même chose avec des commentaires explicatifs:

Code :
  1. import java.lang.String;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. public class ScanWord {
  7.  
  8.    public static void main(String[] args) {
  9.        // D'abord, on récupère les données en entrée
  10.        System.out.print("Enter the sentence: " );
  11.        Scanner inputScanner = new Scanner(System.in);
  12.        // On récupére l'entrée comme une String: inputScanner.nextLine(),
  13.        // On vire les blancs autour: trim()
  14.        String input = inputScanner.nextLine().trim();
  15.        if  (input.length() == 0) {
  16.            inputScanner.close();
  17.            System.out.println("Sentence entered is empty!" );
  18.        }
  19.        else {
  20.            System.out.print("Enter the word: " );
  21.            // On récupére l'entrée et on ne garde que le premier mot
  22.            // Note: On pourrait déclencher une erreur s'il y a plus d'un mot
  23.            String word = inputScanner.nextLine().trim().split("\\s+" )[0];
  24.            inputScanner.close();
  25.            if  (word.length() == 0) {
  26.                System.out.println("Word entered is empty!" );
  27.            }
  28.            else {
  29.                // avec input on construit un String[] en séparant chaque mot: split("\\s+" )
  30.                // On transforme cela en List<String>: Arrays.asList(String[])
  31.                // Bref, on transforme l'entrée en une liste de mots
  32.                List<String> wordList = Arrays.asList(input.trim().split("\\s+" ));
  33.                // On parcourt la liste pour comparer chaque mot a celui a trouver
  34.                int occurences = 0;
  35.                for (String w : wordList) {
  36.                    // Note: ou equalsIgnoreCase selon ce qu'on veut
  37.                    if (w.equals(word)) {
  38.                        ++occurences;
  39.                    }
  40.                }
  41.                // On écrit le résultat
  42.                switch (occurences) {
  43.                case 0:  System.out.println("no \"" + word + "\" in the sentence." );
  44.                    break;
  45.                case 1:  System.out.println("\"" + word + "\" occurs once in the sentence." );
  46.                    break;
  47.                default: System.out.println("\"" + word + "\" occurs " + occurences + " times in the sentence." );
  48.                    break;
  49.  
  50.                }
  51.            }
  52.        }
  53.    }
  54.  
  55. }


 
Note:
Il y avait moyen de faire sans passer par les listes et en restant avec des arrays de strings:

Code :
  1. import java.lang.String;
  2. import java.util.Scanner;
  3.  
  4. public class ScanWord {
  5.  
  6.    public static void main(String[] args) {
  7.        System.out.print("Enter the sentence: " );
  8.        Scanner inputScanner = new Scanner(System.in);
  9.        String input = inputScanner.nextLine().trim();
  10.        if  (input.length() == 0) {
  11.            inputScanner.close();
  12.            System.out.println("Sentence entered is empty!" );
  13.        }
  14.        else {
  15.            System.out.print("Enter the word: " );
  16.            // Note: On pourrait déclencher une erreur s'il y a plus d'un mot
  17.            String word = inputScanner.nextLine().trim().split("\\s+" )[0];
  18.            inputScanner.close();
  19.            if  (word.length() == 0) {
  20.                System.out.println("Word entered is empty!" );
  21.            }
  22.            else {
  23.                String[] wordList = input.trim().split("\\s+" );
  24.                int occurences = 0;
  25.                for (int i = 0; i < wordList.length; ++i) {
  26.                    // Note: ou equalsIgnoreCase selon ce qu'on veut
  27.                    if (wordList[i].equals(word)) {
  28.                        ++occurences;
  29.                    }
  30.                }
  31.                switch (occurences) {
  32.                case 0: System.out.println("no \"" + word + "\" in the sentence." );
  33.                    break;
  34.                case 1: System.out.println("\"" + word + "\" occurs once in the sentence." );
  35.                    break;
  36.                default: System.out.println("\"" + word + "\" occurs " + occurences + " times in the sentence." );
  37.                    break;
  38.  
  39.                }
  40.            }
  41.        }
  42.    }
  43.  
  44. }


Mais bon, autant choisir une structure de données (Liste) qui reflète bien ce que l'on a en tête.
 
A+,


Message édité par gilou le 20-05-2012 à 16:01:09

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed