Trouve présence d'un caractère dans une chaine

Résolu
cs_domxaline Messages postés 327 Date d'inscription jeudi 21 août 2008 Statut Membre Dernière intervention 10 mai 2012 - 28 juil. 2009 à 17:15
cs_domxaline Messages postés 327 Date d'inscription jeudi 21 août 2008 Statut Membre Dernière intervention 10 mai 2012 - 31 juil. 2009 à 08:28
bonjour
je ecris ce prg suivante

import java.util.Scanner;
public class moi
{
public static void main (String [] args)
{
String chaine="Je suis en vacances, oubliez moi un peu";
int nombreDEfois=0;
System.out.print("Entrez un caractère :");
Scanner sc=new.Scanner(System.in);
char c=sc.nextChar;
for(int i=0; i<chaine.length(); i++)
{
if(chaine.charAt(i)==c)
nombreDEfois++;
}
System.out.println("On trouve " +c+ "," +nombreDEfois+ " fois dans: " +"""+chaine+""");
}
}
en compilant il affiche error message suivant
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error on token "new", invalid Expression

at moi.main(moi.java:9)

17 réponses

Utilisateur anonyme
31 juil. 2009 à 01:47
Salut,

je me rend compte que mes 2 réponses ci-dessus pretent à confusion:

La 1ère réponse if(chaine.charAt(i)=='c') c'était pour te montrer la syntaxe. J'aurais dû écrire if(chaine.charAt(i)== monChar), mais comme
scanner donne un String (ou une des primitives) la comparaison String/char n'est pas bonne. Il faudrait donc comparer 2 String, comment:

Avec substring et directement dans chaine:

if (chaine.substring(i, i+1).equals(c))

Ton programme devient donc:

import java.util.Scanner;
public class Moi
// ne pas oublier que par convention un nom de classe commence par une majuscule
{
public static void main (String [] args)
{
String chaine="Je suis en vacances, oubliez moi un peu";
int nombreDEfois=0;
System.out.print("Entrez un caractère :");
Scanner sc=new Scanner(System.in);
String c=sc.nextLine();
for(int i=0; i<chaine.length(); i++)
{
if (chaine.substring(i, i+1).equals(c))
{
nombreDEfois++;
}
}
System.out.println("On trouve " +c+ "," +nombreDEfois+ " fois dans: " +"""+chaine+""");
}
}

Cordialement,


...\ Dan /...
3
cs_domxaline Messages postés 327 Date d'inscription jeudi 21 août 2008 Statut Membre Dernière intervention 10 mai 2012 2
28 juil. 2009 à 20:22
il n'y a personne pour m'aider
0
floufi Messages postés 9 Date d'inscription jeudi 16 juillet 2009 Statut Membre Dernière intervention 15 août 2009
28 juil. 2009 à 21:07
bonjour,

il y a un point après "new" ligne 9
0
cs_domxaline Messages postés 327 Date d'inscription jeudi 21 août 2008 Statut Membre Dernière intervention 10 mai 2012 2
28 juil. 2009 à 21:51
même en changeant comme ça
Scanner sc=new Scanner(System.in);
ça change rien
0

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

Posez votre question
cs_domxaline Messages postés 327 Date d'inscription jeudi 21 août 2008 Statut Membre Dernière intervention 10 mai 2012 2
28 juil. 2009 à 21:55
après avoir corrigé erreur,j'ai nouvelle message suivante

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method nextChar() is undefined for the type Scanner
0
floufi Messages postés 9 Date d'inscription jeudi 16 juillet 2009 Statut Membre Dernière intervention 15 août 2009
28 juil. 2009 à 22:08
c'est un "nextLine" qu'il faut faire et tu recupere un "String"
0
cs_domxaline Messages postés 327 Date d'inscription jeudi 21 août 2008 Statut Membre Dernière intervention 10 mai 2012 2
28 juil. 2009 à 22:38
import java.util.Scanner;
public class moi
{
public static void main (String [] args)
{
String chaine="Je suis en vacances, oubliez moi un peu";
int nombreDEfois=0;
System.out.print("Entrez un caractère :");
Scanner sc=new Scanner(System.in);
String c=sc.nextLine();
for(int i=0; i<chaine.length(); i++)
{
if(chaine.charAt(i)==c)
nombreDEfois++;
}
System.out.println("On trouve " +c+ "," +nombreDEfois+ " fois dans: " +"""+chaine+""");
}
}

en compilant j'ai un message suivant
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Incompatible operand types char and String

la ligne soulign"
if(chaine.charAt(i)==c)
avec le message incompatible operante types char and string
0
floufi Messages postés 9 Date d'inscription jeudi 16 juillet 2009 Statut Membre Dernière intervention 15 août 2009
28 juil. 2009 à 22:47
c.charAt(0) puisque c'est un String
0
floufi Messages postés 9 Date d'inscription jeudi 16 juillet 2009 Statut Membre Dernière intervention 15 août 2009
28 juil. 2009 à 22:49
c.charAt(1) étant le caractère de fin de chaine dans le cas ou un seul caractère est saisi
0
Utilisateur anonyme
29 juil. 2009 à 05:35
Salut,

On peut faire:

------------------
--------------------
String chaine="Je suis en vacances, oubliez moi un peu";


boolean encore = true;
int nombreDEfois=0;
String saisie = "";
String tmp = chaine;
String pasOuPlus = "Il n'y a pas de ";
int ind = 0;
//Supposons que le caractère saisi est "u" et est dans String saisie:
saisie = "u";
while (encore)
{
ind = tmp.indexOf(saisie);
if (ind == -1)
{
System.out.println("\n" + pasOuPlus + """ + saisie + "" dans la chaine:\n"
+ """ + chaine + ""\n");
break;
}
else
{
nombreDEfois++;
pasOuPlus = "Il n'y a plus de ";
tmp = tmp.substring(ind +1);
ind++;
}
}
System.out.println("RESULTAT:\n"
+ "IL Y A " + nombreDEfois + " "" + saisie + "" dans la chaine:\n"
+ """ + chaine + """);

// et on boucle sur la saisie... (qui ici n'est pas représentée)

Cordialement,



...\ Dan /...
0
cs_domxaline Messages postés 327 Date d'inscription jeudi 21 août 2008 Statut Membre Dernière intervention 10 mai 2012 2
29 juil. 2009 à 08:25
if(chaine.charAt(i)==c)
c.charAt(1)
donc je dois ecrire comme ça
if(c.charAt(1))
toujours marche pas
"type mismatche cannot convert from char to boolean"

en demandant compil j'ai error suivant
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from char to boolean
0
Utilisateur anonyme
29 juil. 2009 à 11:09
Si tu veux faire un essais avec ce code:

String chaine="Je suis en vacances, oubliez moi un peu";
String tmp = chaine;

boolean encore = true;
boolean saisir = true;
int nombreDEfois=0;
String yaPas = "Il n'y a pas de ";
int ind = 0;

String saisie = "";

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader flux = new BufferedReader(isr);

while (saisir)
{
System.out.print("\nSaisir la lettre a compter dans la chaine"
+ " (ou intro pour terminer): ");
try
{
saisie =flux.readLine();
}
catch(IOException ioe)
{
System.err.println("Erreur: " + ioe.getMessage());
}

if (saisie.equals(""))
return;

while (encore)
{
ind = tmp.indexOf(saisie);
if (ind == -1)
{
System.out.println("\n" + yaPas + """ + saisie + "" dans la chaine:\n"
+ """ + chaine + ""\n");
break;
}
else
{
nombreDEfois++;
yaPas = "Fin de la recherche de ";
tmp = tmp.substring(ind +1);
ind++;
}
}
System.out.println("RESULTAT:\n"
+ "IL Y A " + nombreDEfois + " "" + saisie + "" dans la chaine:\n"
+ """ + chaine + """);
nombreDEfois = 0;
yaPas = "Il n'y a pas de ";
tmp = chaine;

// et on boucle sur la saisie
}

Il est maintenant complet, saisie comprise.
Si tu veux de l'aide pour comprendre n'hésite pas...

...\ Dan /...
0
cs_domxaline Messages postés 327 Date d'inscription jeudi 21 août 2008 Statut Membre Dernière intervention 10 mai 2012 2
29 juil. 2009 à 21:39
on peut rien faire avec mon prg?
il faut ecrire un nouveau prg?
0
cs_domxaline Messages postés 327 Date d'inscription jeudi 21 août 2008 Statut Membre Dernière intervention 10 mai 2012 2
30 juil. 2009 à 22:07
merci pour danimo
mais excuse moi,est ce que tu peux corriger mon prg s'il te plaît
0
Utilisateur anonyme
30 juil. 2009 à 22:25
Salut,

remplace tout simplement:
if(chaine.charAt(i)==c)

par:



if(chaine.charAt(i)=='c')

Cordialement,

...\ Dan /...
0
Utilisateur anonyme
30 juil. 2009 à 22:34
Ps,

Mais attention, tu dois maintenant voir pourquoi tu as toujours le même résultat >>>>>>>>> 2 !!!


...\ Dan /...
0
cs_domxaline Messages postés 327 Date d'inscription jeudi 21 août 2008 Statut Membre Dernière intervention 10 mai 2012 2
31 juil. 2009 à 08:28
merci beaucoup
if (chaine.substring(i, i+1).equals(c))
cet ligne va bien avec mon prg
prg marche
merci beaucoup encore
0
Rejoignez-nous