Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

Sujet : Suppression des occurences [ Divers / Débutant(e) ] (sarita199)

dimanche 5 octobre 2008 à 02:15:30 | Suppression des occurences

sarita199

Bsr a tous, je suis bloquée dans ce code qui permet de supprimer les occurences(les doublants) d'un mot ou d'une phrase donnée, Exp: football donne fotbal
Avez vous une suggestion!!!

Scanner sc=new Scanner(System.in);
public void AffectationTable()throws Exception
{
 String T1[],T2[];
 int i=0,j=0;
 int l=0,k=0;
 int length=10;
 T1=new String[length];
 T2=new String[length];
 String value;
 do
 {
  System.out.print("Introduire une phrase: ");
     value=sc.next();
     T1[i]=value;
  i++;
 }
 while(!value.equals("."));
 T2[k]="";
 for(j=0;j<length;j++)
 {
  if(T1[j]!=T1[j+1])
   T2[k]=T2[k]+T1[j];
  k++;
 }
  
 for(j=0;j<i-1;j++)
 {System.out.println(T2[j]+"\t");}
}

dimanche 5 octobre 2008 à 11:34:37 | Re : Suppression des occurences

danimo

Réponse acceptée !
Bonjour,

Scanner sc=

new Scanner(System.in);
37 publicvoid AffectationTable()throws Exception
38 {
39 String T1[],T2[];
40 int i=0,j=0;
41 int l=0,k=0;
42 boolean x = false;
43 int isv = 0;
44 int length=10;
45 T1=new String[length];
46 T2=new String[length];
47 // length n'est pas le nombre de caracteres entrés
48 // mais le nombre d'éléments maxi des tableaux T1 et T2
49 // dans cet exemple 10 éléments numerotés 0 a 9
50 // Remarque : il vaudrait mieux nommer T1 et T2 t1 et t2
51 // un nom de class doit commencer par une majuscule(convention)
52 String value = "";
53
54 for (i = 0; i < length; i++)
55 {
56 System.out.print("Introduire une phrase: ");
57 value=sc.nextLine(); // <<<<<<<<<<<<<<<<<<<<<<<<
58 if ((value.equals(".")) || (value.length() == 0))
59 {
60 x = true; // <<<<<<<<<<<<<<<<<<<<<<<<
61 isv = i; // <<<<<<<<<<<<<<<<<<<<<<<<
62 break;
63 }
64 System.out.println("value = " + value);
65 x = false; // <<<<<<<<<<<<<<<<<<<<<<<<
66 T1[i]=value;
67 System.out.println("T1[" + i + "] = " + T1[i]);
68 // ici on peut rechercher les doubles, mais on garde ton idée de le faire
69 // dans la boucle suivante
70 // Remarque : ne pas oublier qu'il y a des mots qui ont des lettres
71 // en bouble (femme, bonnet, attraper...) mais on n'en tiendra pas compte
72 // ce n'est certainement pas le but de l'exercice sinon ....
73 }
74
75 T2[k]="";
76 int lgvalue;
77 String car = "";
78 String carsv = "";
79 String nvalue = "";
80
81 for(j=0;j<length;j++)
82 {
83 if (x) // <<<<<<<<<<<<<<<<<<<<<<<<
84 if (j == isv) // <<<<<<<<<<<<<<<<<<<<<<<<
85 break; // <<<<<<<<<<<<<<<<<<<<<<<<
86 value = T1[j];
87 System.out.println("value2 = " + value);
88 lgvalue = value.length();
89 for (k = 0; k < lgvalue; k++)
90 {
91 car = value.substring(k, k +1);
92 if (!car.equals(carsv))
93 {
94 nvalue = nvalue + car;
95 carsv = car;
96 }
97 }
98 T2[j] = nvalue;
99 nvalue = "";
100 }
101 int fini = length;
102 // pour ne pas lister les T2[j] = null, quitter les // des 2 lignes suivantes
103 //if (x)
104 //fini = isv;
105 for(j=0;j<fini;j++)
106 {
107 System.out.println("T2[" + j + "] = " + T2[j]);
108 }Cordialement.

Dan

dimanche 5 octobre 2008 à 11:58:02 | Re : Suppression des occurences

Ombitious_Developper

Salut:

Ici, j'ai fait le découplage entre la procédure de suppression des doublons et d'extraction des jetons d'une chaine de caractères.

Supprimer les doublons dans un tableau de chaînes de caractères.

public String[] unique(String[] strings) {
    Set<String> set = new HashSet<String>();
    for (String s : strings) {
       set.add(s);
    }
    return s.toArray(new String[0]);
}


pour découper une chaîne de caractères:

public String[] tokenize(String string) {
    String[] tokens;
    if (string == null || string.trim().length() == 0) {
       tokens = new String[0];
    } else {
       StringTokenizer st = new StringTokenizer(string);
       int count = st.countTokens();
       List<String> list = new ArrayList<String>();
       while (st.hasMoreTokens()) {
          list.add(st.nextToken());
       }
       tokens = list.toArray(new String[0]);
    }
    return tokens;
}


Pour faire les deux traitements ensemble:

public String[] tokenize(String string) {
    String[] tokens;
    if (string == null || string.trim().length() == 0) {
       tokens = new String[0];
    } else {
       StringTokenizer st = new StringTokenizer(string);
       int count = st.countTokens();
       Set<String> set = new HashSet<String>();
       while (st.hasMoreTokens()) {
          set.add(st.nextToken());
       }
       tokens = set.toArray(new String[0]);
    }
    return tokens;
}


dimanche 5 octobre 2008 à 11:59:14 | Re : Suppression des occurences

Ombitious_Developper

Salut:

Une correction:

public String[] unique(String[] strings) {
    Set<String> set = new HashSet<String>();
    for (String s : strings) {
       set.add(s);
    }
    return set.toArray(new String[0]);
}


dimanche 5 octobre 2008 à 12:00:27 | Re : Suppression des occurences

Ombitious_Developper

Salut:

public String[] unique(String[] strings) {
    Set<String> set = new HashSet<String>();
    for (String s : strings) {
       set.add(s);
    }
    return set.toArray(new String[0]);
}



public String[] tokenize(String string) {
    String[] tokens;
    if (string == null || string.trim().length() == 0) {
       tokens = new String[0];
    } else {
       StringTokenizer st = new StringTokenizer(string);
       List<String> list = new ArrayList<String>();
       while (st.hasMoreTokens()) {
          list.add(st.nextToken());
       }
       tokens = list.toArray(new String[0]);
    }
    return tokens;
}



public String[] tokenize(String string) {
    String[] tokens;
    if (string == null || string.trim().length() == 0) {
       tokens = new String[0];
    } else {
       StringTokenizer st = new StringTokenizer(string);
       Set<String> set = new HashSet<String>();
       while (st.hasMoreTokens()) {
          set.add(st.nextToken());
       }
       tokens = set.toArray(new String[0]);
    }
    return tokens;
}


dimanche 5 octobre 2008 à 20:07:21 | Re : Suppression des occurences

danimo

Salut,

Effectivement Ombitious_   mais c'est un exercice et j'ai  choisi de rester le plus basic possible.

Cordialement.

...\ Dan /...


lundi 6 octobre 2008 à 01:54:24 | Re : Suppression des occurences

sarita199

Merci pr vous Ombicieus_Developper et damino, les deux méthodes ça marche bien mais j'ai choisi la celle de damino par ce que je travail sur la notion des tableaus et pas des collections:

damino j'ai modifié un petit peux ta méthode pour le rendre breve,mais j'ai une petite question pr toi pr koi on ne peux pas travailler sur une seul boucle???

public void AffectationTable1()throws Exception
{
int i=0,j=0,k=0;
String chain = "",chain1= "",valchain = "",fini="";
boolean trouve = false;
String value = "";
for (i = 0; i < length; i++)
{
   System.out.print("Introduire une phrase: ");
   value=sc.nextLine();
   if ((value.equals(".")) || (value.length() == 0))
  { trouve = true;
     break;}
   T1[i]=value;
}
 
T2[k]="";
int val;
for(j=0;j<length;j++)
{
   if(j == i) //A koi sert il???
     break;
  value = T1[j];
  val = value.length();
  for (k = 0; k < val; k++)
  {
      chain = value.substring(k, k +1);
      if (!chain.equals(chain1))
         {
         valchain = valchain + chain;
           chain1 = chain;
         }
  }
  T2[j] = valchain;
  valchain = "";
 }
// System.out.println("Le nombre d'élément: "+j);
for(int l=0;l<j;l++)
{
    System.out.println(T2[l]);
}


lundi 6 octobre 2008 à 15:36:25 | Re : Suppression des occurences

danimo

Bonjour,

1°-Si on peut avec une seule boucle (je l'ai d'ailleurs indiqué en commentaire)

....
66 T1[i]=value;
67 System.out.println("T1[" + i + "] = "
+ T1[i]);
68
// ici on peut rechercher les doubles, mais on garde ton idée de le faire
69 // dans la boucle suivante
....

2°-
....
   if(j == i) //A koi sert il???
     break;
....

Ce n'est pas ce que j'ai écri mais if(j == isv)  isv est la sauvegarde de i au moment où on a détecté la frappe de "." ou une intro sans frappe, donc la fin des intros.
C'est pour éviter de lire T1 alors qu'il n'y à plus rien d'entré, mais aussi pour éviter une exception ici:
  val = value.length(); alors que val est null.

Mais il ne faut faire ce test que si trouve est true :

if (trouve)
if (j == i)  // j'ai utilisé isv (sauvegarde de i) car on risque un jour de modifier i avant d'arriver ici
break;

ou bien :
if (trouve)
{
if (j == i)
break;
}
ce qui est plus lisible

Cordialement


...\ Dan /...


lundi 6 octobre 2008 à 20:09:29 | Re : Suppression des occurences
mardi 7 octobre 2008 à 01:14:48 | Re : Suppression des occurences

danimo

Bonjour,

Les 2 cas:

1
2

import java.util.*;
3 public class
Exercices2
4
{
5 Scanner sc= new
Scanner(System.in);
6 public void AffectationTable() throws
Exception
7
{
8
String T1[],T2[];
9 int
i=0,j=0;
10 int
l=0,k=0;
11 boolean x = false
;
12 int
isv = 0;
13 int
length=10;
14 T1= new
String[length];
15 T2= new
String[length];
16
// dans cet exemple 10 éléments numerotés 0 a 9
17
// Remarque : il vaudrait mieux nommer T1 et T2 t1 et t2
18
// Seul un nom de class doit commencer par une majuscule(convention)
19 String value = ""
;
20

21 // avec 2 boucles (+ la boucle d'impression)
22

23 System.out.println( "-----------------------\n\nExemple avec 2 boucles\n\n" );
24

25 for (i = 0; i < length; i++)
26
{
27
//nous allons entrer 3 "phrases" 1: aaabc 2: deeef et 3: ghiii
28 System.out.print( "Introduire une phrase: "
);
29
value=sc.nextLine();
30 if ((value.equals( "."
)) || (value.length() == 0))
31
{
32 x = true
;
33
isv = i;
34 break
;
35
}
36 System.out.println( "value = "
+ value);
37 x = false
;
38
T1[i]=value;
39 System.out.println( "T1[" + i + "] = "
+ T1[i]);
40
// ici on peut rechercher les doubles.
41
}
42

43 T2[k]= "" ;
44 int
lgvalue;
45 String car = ""
;
46 String carsv = ""
;
47 String nvalue = ""
;
48

49 for (j=0;j<length;j++)
50
{
51 if
(x)
52
{
53 if
(j == isv)
54 break
;
55
}
56
value = T1[j];
57 System.out.println( "value2 = "
+ value);
58
lgvalue = value.length();
59 for
(k = 0; k < lgvalue; k++)
60
{
61
car = value.substring(k, k +1);
62 if
(!car.equals(carsv))
63
{
64
nvalue = nvalue + car;
65
carsv = car;
66
}
67
}
68
T2[j] = nvalue;
69 nvalue = ""
;
70
}
71 int
fini = length;
72
// pour ne pas lister les T2[j] = null, quitter les // des 2 lignes suivantes
73
//if (x)
74
//fini = isv;
75 for
(j=0;j<fini;j++)
76
{
77 System.out.println( "T2[" + j + "] = "
+ T2[j]);
78
}
79

80 // Avec une seule boucle + la boucle d'impression a la console
81

82 System.out.println( "-----------------------\n\nExemple avec une seule boucle\n\n" );
83

84 T1= new String[length];
85 T2= new
String[length];
86

87 i = 0;
88
j = 0;
89
k = 0;
90 x = false
;
91 carsv = ""
;
92

93 for (i = 0; i < length; i++)
94
{
95
// Nous entrons ci-dessous les memes mots aaabc deeef et ghiii et bie entendu
96
// nous devons obtenir le meme résultat
97

98 System.out.print( "Introduire une phrase: " );
99
value=sc.nextLine();
100 if ((!value.equals( "."
)) && (value.length() != 0))
101
{
102 System.out.println( "value = "
+ value);
103
T1[i]=value;
104 System.out.println( "T1[" + i + "] = "
+ T1[i]);
105 for
(j=0;j<length;j++)
106
{
107 if
(j == i +1)
108 break
;
109
value = T1[j];
110 System.out.println( "value2 = "
+ value);
111
lgvalue = value.length();
112 for
(k = 0; k < lgvalue; k++)
113
{
114
car = value.substring(k, k +1);
115 if
(!car.equals(carsv))
116
{
117
nvalue = nvalue + car;
118
carsv = car;
119
}
120
}
121
T2[j] = nvalue;
122 nvalue = ""
;
123
}
124
}
125
else
126 break
;
127
}
128

129 for (j=0;j<fini;j++)
130
{
131 System.out.println( "T2[" + j + "] = "
+ T2[j]);
132
}
133
}
134

135 /* Nous obtenons:
136
-----------------------
137

138 Exemple avec 2 boucles
139

140
141 Introduire une phrase: 1: aaabc
142
value = 1: aaabc
143
T1[0] = 1: aaabc
144
Introduire une phrase: 2: deeef
145
value = 2: deeef
146
T1[1] = 2: deeef
147
Introduire une phrase: 3" ghiii
148
value = 3" ghiii
149
T1[2] = 3" ghiii
150
Introduire une phrase:
151
value2 = 1: aaabc
152
value2 = 2: deeef
153
value2 = 3" ghiii
154
T2[0] = 1: abc
155
T2[1] = 2: def
156
T2[2] = 3" ghi
157
T2[3] = null
158
T2[4] = null
159
T2[5] = null
160
T2[6] = null
161
T2[7] = null
162
T2[8] = null
163
T2[9] = null
164
-----------------------
165

166 Exemple avec une seule boucle
167

168