Java:word is not present
domxaline
Messages postés
204
Statut
Membre
-
domxaline -
domxaline -
Bonjour,
j'écris ce prg et l'obtient le fausse résultat,
je n'arrive pas trouver mon erreur,aidez moi svp
le résultat est la suivante:
Enter the sentence:
je vais à paris
Enter the word:
paris
word is not present
j'écris ce prg et l'obtient le fausse résultat,
je n'arrive pas trouver mon erreur,aidez moi svp
import java.io.*;
public class Cnt1
{
public static void main(String[]args)throws IOException
{
int times=0,count=0,x=0,no=0;
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
String s,w;
System.out.println("Enter the sentence:");
s=br.readLine();
System.out.println("Enter the word:");
w=br.readLine();
try
{
for(int i=0;i<s.length();i++)
{
if(w.charAt(0)==s.charAt(i))
{
for(int j=0;j<w.length();j++)
{
if(s.charAt(i)==w.charAt(j))
{
count=count+1;
}
if(count==w.length())
{
no=no+1;count=0;
}
else
{
System.out.println("word is present"+no+"times");
}
}}}}
catch(Exception e){}
if(no==0)
{}
{
System.out.println("word is not present");
}
}
}
le résultat est la suivante:
Enter the sentence:
je vais à paris
Enter the word:
paris
word is not present
4 réponses
-
vers la fin, tu as :
catch(Exception e) {}
y a til une erreur avec l espace entre le e et Exeption ?? -
vers la fin, tu as : catch(Exception e) {} y a til une erreur avec l espace entre le e et Exeption ??
je ne pense pas -
Salut,
Le top serait d'utiliser REGEX:
import java.io.*; import java.util.regex.Pattern; import java.util.regex.Matcher; public class Cnt1 { private static int times=0,count=0; public static void main(String[]args)throws IOException { //int times=0,count=0,x=0,no=0; InputStreamReader ir = new InputStreamReader(System.in); BufferedReader br=new BufferedReader(ir); String s,w; System.out.println("Enter the sentence:"); s = br.readLine(); System.out.println("s = " + s); System.out.println("Enter the word:"); w = br.readLine(); System.out.println("w = " + w); Pattern pat = Pattern.compile(w); Matcher matcher = pat.matcher(s); while(matcher.find()) count++; times+= count; System.out.println("\nThe sentence: " + s); System.out.println("On trouve " + times + " fois \"" + w + "\" dans the sentence\n"); if(times == 0) System.out.println("word is not present"); } }
Cordialrment,
Dan -